iPhone:更改 CGImage 的 CGImageAlphaInfo

标签 iphone png alpha cgimage cgbitmapcontextcreate

我的 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/

相关文章:

iphone - cocos2d中的碰撞检测

matlab - 在 MATLAB R2015b 中设置 colorbar 的 alpha

iphone - CIFaceFeature trackingID 对于多张面孔总是相同的

c# - C++ 和 C# 可以用于 iPhone 应用程序吗

c - Deflate:在没有 zlib(或任何其他库)的情况下解压缩 PNG

image - 我可以为 WebGL 纹理使用每 channel 16 位吗

复制到图库的Android透明图像获得纯黑色背景

ios - 启用用户交互的淡入淡出 slider

ios - 我的View是透明的,如何让组件不透明

iphone - 是什么决定了状态栏中是否存在 iPhone 定位服务图标?