【问题标题】:iOS 8 CGContextRef unsupported parameter combinationiOS 8 CGContextRef 不支持的参数组合
【发布时间】:2014-11-06 20:29:45
【问题描述】:

有人知道如何为 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;
}

【问题讨论】:

  • 没人知道怎么做?

标签: ios8 cgcontextref


【解决方案1】:

在上面的 sn-p 中,每行的字节数计算不正确。

要计算每行的字节数,您只需将图像的宽度乘以每像素的位数,在您的情况下似乎是四。

int bytesPerRow = w * 4;

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

【讨论】: