gpt4 book ai didi

iPhone CGContextRef CGBitmapContextCreate 不支持的参数组合

转载 作者:行者123 更新时间:2023-12-03 18:29:23 30 4
gpt4 key购买 nike

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

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

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

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

谢谢你,安德烈

最佳答案

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

就我而言,失败的原因是 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

关于iPhone CGContextRef CGBitmapContextCreate 不支持的参数组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5545600/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com