iPhone CGContextRef CGBitmapContextCreate不支持的参数组合

And*_*csu 10 iphone uiimage cgcontext cgimage cgbitmapcontextcreate

在我的应用程序中,我需要调整大小并裁剪一些本地和在线存储的图像.我正在使用Trevor Harmon的教程实现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.
Run Code Online (Sandbox Code Playgroud)

这是裁剪方法

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

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;
}
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释我有这个问题吗?

谢谢你,安德烈

use*_*609 23

回答这里,因为我得到这个错误时,我有完全相同的像素格式.我希望这个答案有助于某人.

在我的情况下,它失败的原因是它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;
}
Run Code Online (Sandbox Code Playgroud)

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

CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                                newRect.size.width,
                                                newRect.size.height,
                                                CGImageGetBitsPerComponent(imageRef),
                                                0,
                                                CGImageGetColorSpace(imageRef),
                                                CGImageGetBitmapInfo(imageRef));
Run Code Online (Sandbox Code Playgroud)

来源: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的教程](http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/)中的UIImage类别的任何人,代码OP正在使用,这是解决问题的可靠方法. (3认同)

Hlu*_*ung 5

我发现它是色彩空间的问题.只需用CGColorSpaceCreateDeviceRGB()替换CGImageGetColorSpace(imageRef).当我尝试保存从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);
Run Code Online (Sandbox Code Playgroud)


uns*_*zed 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);
}
Run Code Online (Sandbox Code Playgroud)