【问题标题】:<Error>: CGBitmapContextCreate: unsupported parameter combination vs. lower resolution image<错误>:CGBitmapContextCreate:不支持的参数组合与较低分辨率的图像
【发布时间】:2012-02-29 23:12:06
【问题描述】:
- (UIImage *)roundedCornerImage:(NSInteger)cornerSize borderSize:(NSInteger)borderSize {
    // If the image does not have an alpha layer, add one
    UIImage *image = [self imageWithAlpha];

    // Build a context that's the same dimensions as the new size
    CGBitmapInfo info = CGImageGetBitmapInfo(image.CGImage);
    CGContextRef context = CGBitmapContextCreate(NULL,
                                             image.size.width,
                                             image.size.height,
                                             CGImageGetBitsPerComponent(image.CGImage),
                                             0,
                                             CGImageGetColorSpace(image.CGImage),
                                             CGImageGetBitmapInfo(image.CGImage));

    // Create a clipping path with rounded corners
    CGContextBeginPath(context);
    [self addRoundedRectToPath:CGRectMake(borderSize, borderSize, image.size.width - borderSize * 2, image.size.height - borderSize * 2)
                   context:context
                 ovalWidth:cornerSize
                ovalHeight:cornerSize];
    CGContextClosePath(context);
    CGContextClip(context);

    // Draw the image to the context; the clipping path will make anything outside the rounded rect transparent
    CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);

    // Create a CGImage from the context
    CGImageRef clippedImage = CGBitmapContextCreateImage(context);
    CGContextRelease(context);

    // Create a UIImage from the CGImage
    UIImage *roundedImage = [UIImage imageWithCGImage:clippedImage];
    CGImageRelease(clippedImage);

    return roundedImage;
}

我有上述方法,并正在为 Twitter 个人资料图片添加圆角。对于大多数图像,这都很棒。有几个会导致出现以下错误:

:CGBitmapContextCreate:不支持的参数组合:8个整数位/分量; 32 位/像素;三分量色彩空间; kCGImageAlphaLast; 96 字节/行。

我已经进行了一些调试,看起来与导致错误的图像的唯一区别是创建上下文时的参数 CGImageGetBitmapInfo(image.CGImage)。这会引发错误并导致上下文为空。我尝试将最后一个参数设置为 kCGImageAlphaPremultipliedLast 也无济于事。这次绘制了图像,但质量要低得多。有没有办法获得与其他人相提并论的更高质量的图像?图片的路径是通过 Twitter,所以不确定它们是否有不同的可以拉取。

我也看到了有关此错误的其他问题。都没有解决这个问题。我看到了this post,但之后错误的图像完全模糊了。并且将宽度和高度转换为 NSInteger 也不起作用。下面是两张个人资料图像及其质量的屏幕截图。第一个导致错误。

有人知道这是什么问题吗?

非常感谢。这让我很生气。

【问题讨论】:

  • imageWithAlpha 返回的原始图像是否有一个不是 1.0 的scale 值?
  • 模糊图像或错误图像的比例值为 2.0,而其他图像的比例值为 1.0。希望这会有所帮助...

标签: iphone ios


【解决方案1】:

iOS 不支持kCGImageAlphaLast。您需要使用kCGImageAlphaPremultipliedLast

您还需要处理初始图像的比例。您当前的代码没有,因此如果图像的比例为 2.0,它会对图像进行下采样。

您可以使用 UIKit 函数和类更简单地编写整个函数。 UIKit 将为您处理规模;当您要求它创建图形上下文时,您只需要传递原始图像的比例即可。

- (UIImage *)roundedCornerImage:(NSInteger)cornerSize borderSize:(NSInteger)borderSize {
    // If the image does not have an alpha layer, add one
    UIImage *image = [self imageWithAlpha];

    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale); {
        CGRect imageRect = (CGRect){ CGPointZero, image.size };
        CGRect borderRect = CGRectInset(imageRect, borderSize, borderSize);
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:borderRect
            byRoundingCorners:UIRectCornerAllCorners
            cornerRadii:CGSizeMake(cornerSize, cornerSize)];
        [path addClip];
        [image drawAtPoint:CGPointZero];
    }
    UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return roundedImage;
}

如果您的 imageWithAlpha 方法本身从另一个 UIImage 创建了一个 UIImage,它还需要传播比例。

【讨论】:

  • 图像仍然模糊。我从 Twitter 获得的图像是其他图像的一半。有没有办法获得更高的分辨率或更大的图像?或者我该如何处理它的质量?
  • 唯一的问题是圆角的圆角半径是 image.scale 的两倍。我会处理好这件事的。再次感谢。
  • 非常感谢! “iOS 不支持 kCGImageAlphaLast”——截至 2013 年 8 月,标题中没有记录,官方文档(CG 编程指南)中也没有记录。苹果公司的某个人在移植这个时搞砸了,并且没有记录他们删除的功能。注意:在 iOS6 上,您在 Apple 的控制台上收到不正确的错误消息 :(
猜你喜欢
  • 1970-01-01
  • 2011-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多