我的 PNG 图像具有不受支持的位图图形上下文像素格式。每当我尝试调整图像大小时,CGBitmapContextCreate()
就会因不支持的格式而卡住
我收到以下错误(错误格式便于阅读):
CGBitmapContextCreate: unsupported parameter combination:
8 integer bits/component;
32 bits/pixel;
3-component colorspace;
kCGImageAlphaLast;
1344 bytes/row.
list of supported pixel formats绝对不支持这种组合。看来我需要重新绘制图像并将 Alpha channel 信息移动到 kCGImageAlphaPremultipliedFirst or kCGImageAlphaPremultipliedLast .
我不知道该怎么做。
PNG 文件没有任何异常,也没有损坏。它在所有其他环境下都工作得很好。我偶然遇到此错误,但显然我的用户可能有类似格式的文件,因此我必须检查我的应用程序的导入图像并纠正此问题。
最佳答案
是的,我在使用 8 位(索引).PNG 时遇到了问题。我必须将其转换为更原生的图像才能执行图形操作。我基本上做了这样的事情:
- (UIImage *) normalize {
CGColorSpaceRef genericColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef thumbBitmapCtxt = CGBitmapContextCreate(NULL,
self.size.width,
self.size.height,
8, (4 * self.size.width),
genericColorSpace,
kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(genericColorSpace);
CGContextSetInterpolationQuality(thumbBitmapCtxt, kCGInterpolationDefault);
CGRect destRect = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextDrawImage(thumbBitmapCtxt, destRect, self.CGImage);
CGImageRef tmpThumbImage = CGBitmapContextCreateImage(thumbBitmapCtxt);
CGContextRelease(thumbBitmapCtxt);
UIImage *result = [UIImage imageWithCGImage:tmpThumbImage];
CGImageRelease(tmpThumbImage);
return result;
}
关于iPhone:更改 CGImage 的 CGImageAlphaInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2457116/