iPhone:更改 CGImage 的 CGImageAlphaInfo
我的 PNG 图像具有不受支持的位图图形上下文像素格式。每当我尝试调整图像大小时, CGBitmapContextCreate()
就会因不受支持的格式而卡住,
会收到以下错误(错误格式为便于阅读):
CGBitmapContextCreate: unsupported parameter combination:
8 integer bits/component;
32 bits/pixel;
3-component colorspace;
kCGImageAlphaLast;
1344 bytes/row.
我 com/iphone/library/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html#//apple_ref/c/tdef/CGImageAlphaInfo" rel="noreferrer">支持的像素格式列表绝对不支持此组合。看来我需要重新绘制图像并将 Alpha 通道信息移动到 kCGImageAlphaPremultipliedFirst 或 kCGImageAlphaPremultipliedLast。
我不知道该怎么做。
PNG 文件没有任何异常,也没有损坏。它在所有其他环境下都工作得很好。我偶然遇到此错误,但显然我的用户可能有类似格式的文件,因此我必须检查我的应用程序的导入图像并纠正此问题。
I have a PNG image that has an unsupported bitmap graphics context pixel format. Whenever I attempt to resize the image, CGBitmapContextCreate()
chokes on the unsupported format
I receive the following error (error formatted for easy reading):
CGBitmapContextCreate: unsupported parameter combination:
8 integer bits/component;
32 bits/pixel;
3-component colorspace;
kCGImageAlphaLast;
1344 bytes/row.
The list of supported pixel formats definitely does not support this combination. It appears I need to redraw the image and move the alpha channel information to kCGImageAlphaPremultipliedFirst or kCGImageAlphaPremultipliedLast.
I have no idea how to go about doing this.
There is nothing unusual about the PNG file and it isn't corrupted. It works in all other context just fine. I encountered this error just by chance but obviously my users might have similarly formatted files so I will have to check my app's imported images and correct for this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,我在使用 8 位(索引).PNG 时遇到了问题。我必须将其转换为更原生的图像才能执行图形操作。我基本上做了这样的事情:
Yeah, I had problems with 8 bit (indexed) .PNGs. I had to convert it to a more native image to perform graphics operations. I essentially did something like this:
这是 Alfons 答案中的方法的更新版本,用于考虑屏幕比例,以及图像大小的浮点值中的一些愚蠢的错误,如原始答案中的 unsynchronized 评论中所述。
SCREEN_SCALE 是一个宏,如果未定义比例,则返回 1.0,或者返回实际的设备比例 ([UIScreen mainScreen].scale)。
Here's an updated version of the method from Alfons answer to account for screen scale, and also some silly errors with decimals in floating point values of the image size as described in unsynchronized's comment from the original answer.
SCREEN_SCALE is a macro that returns either 1.0 if scale isn't defined or whatever the device scale actually is ([UIScreen mainScreen].scale).