10

在我的应用程序中,我需要调整并裁剪一些本地和在线存储的图像。我正在使用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.

这是裁剪方法

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

调整大小的方法是这样的:

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

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

谢谢你,安德烈

4

5 回答 5

24

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

在我的情况下,它失败的原因是它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

于 2014-10-14T16:00:45.900 回答
5

我发现这是色彩空间的问题。只需将 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);
于 2011-12-10T08:12:46.237 回答
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);
}
于 2011-10-23T20:30:37.483 回答
3

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

我正在使用这里找到的非常流行的类别。

这个博客上,人们遇到了同样的问题。马特在 2011 年 11 月 22 日的回答帮助了我。

干杯!

于 2013-06-11T08:30:01.420 回答
0

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

于 2013-08-17T18:37:45.790 回答