2010-03-16 75 views
25

我有一个具有不受支持的位图图形上下文像素格式的PNG图像。每当我试图调整图像大小,CGBitmapContextCreate()扼流圈上不支持的格式iPhone:更改CGImage的CGImageAlphaInfo

我收到以下错误(错误格式,方便阅读):

CGBitmapContextCreate: unsupported parameter combination: 
    8 integer bits/component; 
    32 bits/pixel; 
    3-component colorspace; 
    kCGImageAlphaLast; 
    1344 bytes/row. 

list of supported pixel formats绝对不支持这种组合。看起来我需要重新绘制图像并将Alpha通道信息移动到kCGImageAlphaPremultipliedFirst or kCGImageAlphaPremultipliedLast

我不知道如何去这样做。

没有什么不寻常的PNG文件,并没有损坏。它适用于所有其他环境。我偶然遇到了这个错误,但显然我的用户可能有格式相似的文件,所以我将不得不检查我的应用程序导入的图像并更正此问题。

回答

56

是啊,我有8位(索引).PNGs问题。我不得不将它转换为更原生的图像来执行图形操作。在不同步的评论描述

​​
+0

看起来不错,我来试试。我不喜欢做很多处理,因为我害怕我会不经意间破坏忠诚。 – TechZen 2010-03-21 03:58:53

+0

我终于开始测试这个,它工作正常,没有明显的保真度损失。 – TechZen 2010-03-26 03:52:06

+0

当UIImage的imageOrientation是除了OrientationUp之外的任何东西时,我遇到了上述代码的问题 - 这会导致图像旋转。图像的边界保持不变,但图像内的像素被旋转和拉伸以适应原始边界。这与以下内容类似:http://stackoverflow.com/questions/5973105/image-clicked-from-iphone-in-portrait-mode-gets-rotated-by-90-degree,但在这种情况下,它是规范化看起来导致问题的功能。其他人看到这个?任何解决这个问题? – kurtzmarc 2011-10-02 22:24:14

5

下面是从阿尔方回答方法的更新版本,以帐户为屏幕比例,同时在浮动图像大小的点值与小数一些愚蠢的错误:我基本上是做了这样的事情从原来的答案。

SCREEN_SCALE是返回宏任1.0如果规模没有定义或任何装置规模实际上是([UIScreen mainScreen] .scale)。

- (UIImage *) normalize { 

    CGSize size = CGSizeMake(round(self.size.width*SCREEN_SCALE), round(self.size.height*SCREEN_SCALE)); 
    CGColorSpaceRef genericColorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef thumbBitmapCtxt = CGBitmapContextCreate(NULL, 
                 size.width, 
                 size.height, 
                 8, (4 * size.width), 
                 genericColorSpace, 
                 kCGImageAlphaPremultipliedFirst); 
    CGColorSpaceRelease(genericColorSpace); 
    CGContextSetInterpolationQuality(thumbBitmapCtxt, kCGInterpolationDefault); 
    CGRect destRect = CGRectMake(0, 0, size.width, size.height); 
    CGContextDrawImage(thumbBitmapCtxt, destRect, self.CGImage); 
    CGImageRef tmpThumbImage = CGBitmapContextCreateImage(thumbBitmapCtxt); 
    CGContextRelease(thumbBitmapCtxt);  
    UIImage *result = [UIImage imageWithCGImage:tmpThumbImage scale:SCREEN_SCALE orientation:UIImageOrientationUp]; 
    CGImageRelease(tmpThumbImage); 

    return result;  
}