苹果再一次调皮了。
- (void)drawRect:(CGRect)dirtyRect
{
CGRect bounds = [self bounds];
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
size_t bytesPerRow = 4 * roundf(bounds.size.width);
context = CGBitmapContextCreate(NULL, roundf(bounds.size.width), roundf(bounds.size.height), 8, bytesPerRow, space, kCGBitmapAlphaInfoMask);
// ...
}
大概上面这样的代码,在更新到iOS7之前貌似没什么问题。现在提示
<Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; unrecognized; 864 bytes/row.
在网上找了很久资料,很多人是在crop图片时才会遇到这个问题。至于原因,bytesPerRow的计算方式不对,CGBitmapContextCreate的第二个参数float,bytesPerRow是整数,会有精度损失。也可以给bytesPerRow赋值0,让系统自己计算。
我遇到的这个问题,找了好久,终于看到一篇切中要害的帖子http://lists.apple.com/archives/carbon-dev/2007/Jun/msg00014.html。
原因是Color sapce和Bitmap info不匹配。解决方案挺简单的,把kCGBitmapAlphaInfoMask替换成kCGImageAlphaPremultipliedFirst。