UIImage使用png-8创建位图上下文

时间:2013-10-09 09:19:43

标签: ios uiimage

您好我正在使用

中的代码调整图片大小

http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

- (UIImage *)resizedImage:(CGSize)newSize
                transform:(CGAffineTransform)transform
           drawTransposed:(BOOL)transpose
     interpolationQuality:(CGInterpolationQuality)quality {
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
    CGRect transposedRect = CGRectMake(0, 0, newRect.size.height, newRect.size.width);
    CGImageRef imageRef = self.CGImage;

    CGBitmapInfo bitMapInfo = CGImageGetBitmapInfo(imageRef);

    // Build a context that's the same dimensions as the new size
    CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                                newRect.size.width,
                                                newRect.size.height,
                                                CGImageGetBitsPerComponent(imageRef),
                                                0,
                                                CGImageGetColorSpace(imageRef),
                                                bitMapInfo);

    // Rotate and/or flip the image if required by its orientation
    CGContextConcatCTM(bitmap, transform);

    // Set the quality level to use when rescaling
    CGContextSetInterpolationQuality(bitmap, quality);

    // Draw into the context; this scales the image
    CGContextDrawImage(bitmap, transpose ? transposedRect : newRect, imageRef);

    // Get the resized image from the context and a UIImage
    CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

    // Clean up
    CGContextRelease(bitmap);
    CGImageRelease(newImageRef);

    return newImage;
}

正常图像正常工作。但是,当我尝试给它一个png-8图像时,它失败了。我知道它是在命令行中输入file image.png的png-8图像。 输出是

image.png: PNG image data, 800 x 264, 8-bit colormap, non-interlaced

控制台中的错误消息是不支持颜色空间。

经过一些谷歌搜索,我意识到“位图图形上下文不支持索引颜色空间。”

根据一些建议,我将其更改为

,而不是使用原始颜色空间
colorSpace = CGColorSpaceCreateDeviceRGB();

现在我收到了这个新错误:

CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 24 bits/pixel; 3-component color space; kCGImageAlphaNone; 2400 bytes/row.

仅供参考,我的图像宽800像素。

如何解决此问题?非常感谢!

1 个答案:

答案 0 :(得分:0)