gpt4 book ai didi

ios - CGContext 错误仅在模拟器中?

转载 作者:行者123 更新时间:2023-11-29 03:43:53 25 4
gpt4 key购买 nike

在测试我的应用程序是否存在任何错误时,我注意到在模拟器中,当我调用其中一个函数将图像设置为 UIImageView 时,会抛出一堆 CGContext 错误,并且图像永远不会被设置。然而,在我的设备上(我在 iPhone 5、iPhone 3GS 和 iPad 3 上进行了测试),这些 CGContext 错误都没有出现,并且图像设置得很好。

无论如何,我寻找了任何可以实现这一点的 CGContext 代码,以下是涉及 CGContext 的唯一方法:

- (void)setCropRect:(CGRect)cropRect
{
if(!CGRectEqualToRect(_cropRect,cropRect)){
_cropRect = cropRect;
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.f);
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor blackColor] setFill];
UIRectFill(self.bounds);
CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] colorWithAlphaComponent:0.5].CGColor);
CGContextStrokeRect(context, _cropRect);
[[UIColor clearColor] setFill];
UIRectFill(CGRectInset(_cropRect, 1, 1));
self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
}
}

- (CGImageRef)newScaledImage:(CGImageRef)source withOrientation:(UIImageOrientation)orientation toSize:(CGSize)size withQuality:(CGInterpolationQuality)quality
{
CGSize srcSize = size;
CGFloat rotation = 0.0;

switch(orientation)
{
case UIImageOrientationUp: {
rotation = 0;
} break;
case UIImageOrientationDown: {
rotation = M_PI;
} break;
case UIImageOrientationLeft:{
rotation = M_PI_2;
srcSize = CGSizeMake(size.height, size.width);
} break;
case UIImageOrientationRight: {
rotation = -M_PI_2;
srcSize = CGSizeMake(size.height, size.width);
} break;
default:
break;
}

CGContextRef context = CGBitmapContextCreate(NULL,
size.width,
size.height,
8, //CGImageGetBitsPerComponent(source),
0,
CGImageGetColorSpace(source),
kCGImageAlphaNoneSkipFirst//CGImageGetBitmapInfo(source)
);

CGContextSetInterpolationQuality(context, quality);
CGContextTranslateCTM(context, size.width/2, size.height/2);
CGContextRotateCTM(context,rotation);

CGContextDrawImage(context, CGRectMake(-srcSize.width/2 ,
-srcSize.height/2,
srcSize.width,
srcSize.height),
source);

CGImageRef resultRef = CGBitmapContextCreateImage(context);
CGContextRelease(context);

return resultRef;
}

- (CGImageRef)newTransformedImage:(CGAffineTransform)transform
sourceImage:(CGImageRef)sourceImage
sourceSize:(CGSize)sourceSize
sourceOrientation:(UIImageOrientation)sourceOrientation
outputWidth:(CGFloat)outputWidth
cropSize:(CGSize)cropSize
imageViewSize:(CGSize)imageViewSize
{
CGImageRef source = [self newScaledImage:sourceImage
withOrientation:sourceOrientation
toSize:sourceSize
withQuality:kCGInterpolationNone];

CGFloat aspect = cropSize.height/cropSize.width;
CGSize outputSize = CGSizeMake(outputWidth, outputWidth*aspect);

CGContextRef context = CGBitmapContextCreate(NULL,
outputSize.width,
outputSize.height,
CGImageGetBitsPerComponent(source),
0,
CGImageGetColorSpace(source),
CGImageGetBitmapInfo(source));
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
CGContextFillRect(context, CGRectMake(0, 0, outputSize.width, outputSize.height));

CGAffineTransform uiCoords = CGAffineTransformMakeScale(outputSize.width/cropSize.width,
outputSize.height/cropSize.height);
uiCoords = CGAffineTransformTranslate(uiCoords, cropSize.width/2.0, cropSize.height/2.0);
uiCoords = CGAffineTransformScale(uiCoords, 1.0, -1.0);
CGContextConcatCTM(context, uiCoords);

CGContextConcatCTM(context, transform);
CGContextScaleCTM(context, 1.0, -1.0);

CGContextDrawImage(context, CGRectMake(-imageViewSize.width/2.0,
-imageViewSize.height/2.0,
imageViewSize.width,
imageViewSize.height)
,source);

CGImageRef resultRef = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGImageRelease(source);
return resultRef;
}

这些是 CGContext 错误:

Jul 31 23:37:37 app[10190] <Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 16 bits/pixel; 1-component color space; kCGImageAlphaNoneSkipFirst; 1504 bytes/row.
Jul 31 23:37:37 app[10190] <Error>: CGContextSetInterpolationQuality: invalid context 0x0
Jul 31 23:37:37 app[10190] <Error>: CGContextTranslateCTM: invalid context 0x0
Jul 31 23:37:37 app[10190] <Error>: CGContextRotateCTM: invalid context 0x0
Jul 31 23:37:37 app[10190] <Error>: CGContextDrawImage: invalid context 0x0
Jul 31 23:37:37 app[10190] <Error>: CGBitmapContextCreateImage: invalid context 0x0
Jul 31 23:37:37 app[10190] <Error>: CGBitmapContextCreate: unsupported parameter combination: 0 integer bits/component; 0 bits/pixel; 0-component color space; kCGImageAlphaNone; 0 bytes/row.
Jul 31 23:37:37 app[10190] <Error>: CGContextSetFillColorWithColor: invalid context 0x0
Jul 31 23:37:37 app[10190] <Error>: CGContextFillRects: invalid context 0x0
Jul 31 23:37:37 app[10190] <Error>: CGContextConcatCTM: invalid context 0x0
Jul 31 23:37:37 app[10190] <Error>: CGContextConcatCTM: invalid context 0x0
Jul 31 23:37:37 app[10190] <Error>: CGContextScaleCTM: invalid context 0x0
Jul 31 23:37:37 app[10190] <Error>: CGContextDrawImage: invalid context 0x0
Jul 31 23:37:37 app[10190] <Error>: CGBitmapContextCreateImage: invalid context 0x0

这些是我的应用程序中唯一使用 CGContexts 的方法。但就像我说的,这只发生在模拟器中,而不是在我测试过的设备上。

无论如何,这有什么特殊原因吗?或者有什么方法可以解决这个问题吗?

谢谢大家!

CGContextRef context = CGBitmapContextCreate(NULL,
size.width,
size.height,
CGImageGetBitsPerComponent(source), //8,
0,
CGImageGetColorSpace(source),
CGImageGetBitmapInfo //kCGImageAlphaNoneSkipFirst(source)
);

最佳答案

最有可能的是,您第一次调用 CGBitmapContextCreate失败了,因为你要求它做一些它不能做的事情。您可以在第一条错误消息中看到:

CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 16 bits/pixel; 1-component color space; kCGImageAlphaNoneSkipFirst; 1504 bytes/row.

CGBitmapContextCreate返回 NULL,其余的失败从那里级联。 (您不能在 NULL 上下文上进行操作,并且在 NULL 上下文上调用 CGBitmapContextGetImage() 会为您提供一个 NULL 图像,同样您也无法对其执行任何有用的操作。)

CGBitmapContextCreate 的文档状态:

For the list of supported pixel formats, see “Supported Pixel Formats” in the “Graphics Contexts” chapter of Quartz 2D Programming Guide.

这是direct link到该列表。

关于ios - CGContext 错误仅在模拟器中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17985167/

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