【问题标题】:iPhone CGContextRef CGBitmapContextCreate unsupported parameter combinationiPhone CGContextRef CGBitmapContextCreate 不支持的参数组合
【发布时间】:2011-04-04 23:44:53
【问题描述】:

在我的应用程序中,我需要调整并裁剪一些本地和在线存储的图像。我正在使用Trevor Harmon's tutorial,它实现了UIImage+Resize

在我的 iPhone 4(iOS 4.3.1) 上一切正常,我没有问题。但在我的 iPhone 3G (iOS 3.2) 上,调整大小和裁剪方法不适用于任何图片(本地存储的是 PNG)。 这是控制台输出:

Tue Apr  5 02:34:44 Andreis-MacBook-Pro.local Puzzle[12453] <Error>: CGBitmapContextCreate:     unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; kCGImageAlphaLast; 288 bytes/row.
Tue Apr  5 02:34:44 Andreis-MacBook-Pro.local Puzzle[12453] <Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; kCGImageAlphaLast; 288 bytes/row.
Tue Apr  5 02:34:44 Andreis-MacBook-Pro.local Puzzle[12453] <Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; kCGImageAlphaLast; 288 bytes/row.
Tue Apr  5 02:34:44 Andreis-MacBook-Pro.local Puzzle[12453] <Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; kCGImageAlphaLast; 288 bytes/row.

这是裁剪方法

- (UIImage *)croppedImage:(CGRect)bounds 
{
    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], bounds);
    UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return croppedImage;
}

resize方法是这样的:

- (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;

    CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                            newRect.size.width,
                                            newRect.size.height,
                                            CGImageGetBitsPerComponent(imageRef),
                                            0,
                                            CGImageGetColorSpace(imageRef),
                                            CGImageGetBitmapInfo(imageRef));
    if(bitmap == nil)
        return nil;

    CGContextConcatCTM(bitmap, transform);

    CGContextSetInterpolationQuality(bitmap, quality);

    CGContextDrawImage(bitmap, transpose ? transposedRect : newRect, imageRef);

    CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

    CGContextRelease(bitmap);
    CGImageRelease(newImageRef);

    return newImage;
}

有人可以向我解释一下我遇到这个问题的方式吗?

谢谢你, 安德烈

【问题讨论】:

    标签: iphone uiimage cgcontext cgimage cgbitmapcontextcreate


    【解决方案1】:

    在这里回复是因为我收到此错误时的像素格式完全相同。我希望这个答案对某人有所帮助。

    在我的情况下,它失败的原因是 kCGImageAlphaLast 在 iOS 8 上不再是允许的值,尽管它在 iOS 7 上运行良好。32 pbb,8 bpc 组合只允许 kCGImageAlphaNoneSkip*kCGImageAlphaPremultiplied* 用于 Alpha 信息。显然这一直是个问题,但在 iOS 8 之前并未强制执行。这是我的解决方案:

    - (CGBitmapInfo)normalizeBitmapInfo:(CGBitmapInfo)oldBitmapInfo {
        //extract the alpha info by resetting everything else
        CGImageAlphaInfo alphaInfo = oldBitmapInfo & kCGBitmapAlphaInfoMask;
    
        //Since iOS8 it's not allowed anymore to create contexts with unmultiplied Alpha info
        if (alphaInfo == kCGImageAlphaLast) {
            alphaInfo = kCGImageAlphaPremultipliedLast;
        }
        if (alphaInfo == kCGImageAlphaFirst) {
            alphaInfo = kCGImageAlphaPremultipliedFirst;
        }
    
        //reset the bits
        CGBitmapInfo newBitmapInfo = oldBitmapInfo & ~kCGBitmapAlphaInfoMask;
    
        //set the bits to the new alphaInfo
        newBitmapInfo |= alphaInfo;
    
        return newBitmapInfo;
    }
    

    在我的例子中,失败的代码看起来像这样,其中 imageRef 是从应用程序包加载的 PNG 的 CGImageRef:

    CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                                    newRect.size.width,
                                                    newRect.size.height,
                                                    CGImageGetBitsPerComponent(imageRef),
                                                    0,
                                                    CGImageGetColorSpace(imageRef),
                                                    CGImageGetBitmapInfo(imageRef));
    

    来源: https://stackoverflow.com/a/19345325/3099609

    https://developer.apple.com/library/mac/DOCUMENTATION/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html#//apple_ref/doc/uid/TP30001066-CH203-BCIBHHBB

    【讨论】:

    • 对于使用来自Trevor Harmon's tutorial 的 UIImage 类别的任何人,OP 正在使用的代码,这是修复它的可靠方法。
    【解决方案2】:

    我发现这是色彩空间的问题。只需将 CGImageGetColorSpace(imageRef) 替换为 CGColorSpaceCreateDeviceRGB() 。当我尝试保存从 AVCaptureSession 获得的图像时,这对我有用。并且不要忘记发布它!

    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                                newRect.size.width,
                                                newRect.size.height,
                                                CGImageGetBitsPerComponent(imageRef),
                                                0,
                                                rgbColorSpace,//CGImageGetColorSpace(imageRef), sometimes contains unsupported colorspace
                                                bitmapInfo);
    CGColorSpaceRelease(rgbColorSpace);
    

    【讨论】:

      【解决方案3】:

      好的,这可能对你有帮助,也可能对你没有帮助(我很欣赏这是一篇旧帖子)

      我遇到了类似的问题,因为宽度参数(在您的情况下为 newRect.size.width)有一个小数部分(例如 100.001 而不是 100.0)。我将其类型转换为整数并返回,截断小数部分,问题就消失了。我猜有一个测试可以看到每个分量x像素等的位数加起来,它不能处理分数像素/点。如果有帮助,欢迎您使用此方法。

      +(CGSize)  fixSize:(CGSize) forSize{
          NSInteger w = (NSInteger) forSize.width;
          NSInteger h = (NSInteger) forSize.height;
          return CGSizeMake(w, h);
      }
      

      【讨论】:

      • 我在 iOS 6 上观察到了完全相同的错误。这条评论可能为我节省了漫长的颠簸旅程。
      【解决方案4】:

      我在 iOS 5 模拟器上遇到了同样的问题,上面的答案并没有解决我的问题:图像没有加载,控制台仍然报告同样的错误。

      我正在使用here 找到的非常受欢迎的类别。

      this blog 上,人们遇到了同样的问题。 Matt 在 2011 年 11 月 22 日的回答帮助了我。

      干杯!

      【讨论】:

        【解决方案5】:

        从 5.1 切换到 6.1 进行测试时,我在模拟器中看到了这个问题。关闭模拟器并再次打开它似乎已经消除了错误。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-09
          相关资源
          最近更新 更多