解决 CGBitmapContextCreate: unsupported parameter combination 错误

苹果再一次调皮了。

- (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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值