【问题标题】:UIImage create bitmap context with png-8UIImage 使用 png-8 创建位图上下文
【发布时间】:2013-10-10 16:00:13
【问题描述】:

您好,我正在使用来自

的代码调整图像大小

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

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

【问题讨论】:

    标签: ios uiimage


    【解决方案1】:

    【讨论】:

      猜你喜欢
      • 2010-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-09
      • 2014-07-27
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      相关资源
      最近更新 更多