iOS 8 CGContextRef不支持的参数组合

时间:2014-11-06 20:29:45

标签: ios8 cgcontextref

任何人都知道如何为iOS 8更新此代码?我收到此错误消息:

CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; kCGImageAlphaPremultipliedFirst; 4294967289 bytes/row.

CGContextRef CreateBitmapContenxtFromSizeWithData(CGSize s, void* data)
{
    int w = s.width, h = s.height;
    int bitsPerComponent = 8;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    int components = 4;
    int bytesPerRow = (w * bitsPerComponent * components + 7)/8;

    CGContextRef result = CGBitmapContextCreate(data, w, h, 8, bytesPerRow, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedFirst);
    CGColorSpaceRelease(colorSpace);
    return result;
}

1 个答案:

答案 0 :(得分:0)

在上面的代码段中,每行的字节数计算错误。

要计算每行的字节数,您只需获取图像的宽度,然后将其乘以每个像素的位数,在您的情况下似乎为4。

int bytesPerRow = w * 4;

但请注意,如果data指向存储在RGB中的图像数据,则每个像素有三个字节。您还需要将CGImageAlphaInfo.NoneSkipFirst标志作为最后一个参数传递给CGBitmapContextCreate,以便省略alpha通道。