0

嗨,我正在使用来自的代码调整图像大小

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 像素宽。

我该如何解决这个问题?非常感谢!

4

1 回答 1

0

我意识到支持的格式列表在这里: https ://developer.apple.com/library/ios/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html#//apple_ref/doc/uid/TP30001066-CH203 -BCIBHHBB

并且没有 24 位/像素。

所以我在这里结束了使用公认的解决方案: iPhone:Changing CGImageAlphaInfo of CGImage

于 2013-10-10T08:00:13.893 回答