CGContext& CGBitmapContextCreateImage错误,如何修复? [英] CGContext & CGBitmapContextCreateImage errors, how to fix?

查看:278
本文介绍了CGContext& CGBitmapContextCreateImage错误,如何修复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这几个小时了,甚至不知道如何调试此错误。也许有一位SO专家知道发生了什么。

I've been at this for hours now, and don't even know how to debug this error. Maybe there's an SO expert who knows what's going on.

- (void) prepareSubset {

CGSize size = [image size];

float scale = fminf(1.0f, fmaxf(SUBSET_SIZE / cropRect.size.width, SUBSET_SIZE / cropRect.size.height));
CGPoint offset = CGPointMake(-cropRect.origin.x, -cropRect.origin.y);

subsetWidth = cropRect.size.width * scale;
subsetHeight = cropRect.size.height * scale;

subsetBytesPerRow = ((subsetWidth + 0xf) >> 4) << 4;
subsetData = (unsigned char *)malloc(subsetBytesPerRow * subsetHeight);

CGColorSpaceRef grayColorSpace = CGColorSpaceCreateDeviceGray();

CGContextRef ctx = CGBitmapContextCreate(subsetData, subsetWidth, subsetHeight, 8, subsetBytesPerRow, grayColorSpace, kCGImageAlphaNone);
CGColorSpaceRelease(grayColorSpace);
CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);
CGContextSetAllowsAntialiasing(ctx, false);

CGContextTranslateCTM(ctx, 0.0, subsetHeight);
CGContextScaleCTM(ctx, 1.0, -1.0);  

UIGraphicsPushContext(ctx);
CGRect rect = CGRectMake(offset.x * scale, offset.y * scale, scale * size.width, scale * size.height);
[image drawInRect:rect];
UIGraphicsPopContext();

CGContextFlush(ctx);

CGImageRef subsetImageRef = CGBitmapContextCreateImage(ctx);

self.subsetImage = [UIImage imageWithCGImage:subsetImageRef];
CGImageRelease(subsetImageRef);

CGContextRelease(ctx);

}

这些是错误我打电话给这个方法后出现

These are the errors that come up once I call this method

<Error>: CGContextSetInterpolationQuality: invalid context 0x0
<Error>: CGContextSetAllowsAntialiasing: invalid context 0x0
<Error>: CGContextTranslateCTM: invalid context 0x0
<Error>: CGContextScaleCTM: invalid context 0x0
<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextSetBlendMode: invalid context 0x0
<Error>: CGContextSetAlpha: invalid context 0x0
<Error>: CGContextTranslateCTM: invalid context 0x0
<Error>: CGContextScaleCTM: invalid context 0x0
<Error>: CGContextDrawImage: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0
<Error>: CGContextFlush: invalid context 0x0
<Error>: CGBitmapContextCreateImage: invalid context 0x0

任何人都可以请告诉我发生了什么事?在此先感谢。

Can anyone PLEASE tell me what's going on? Thanks in advance.

推荐答案

如果创建上下文时出错,CGBitmapContextCreate将返回NULL,因此这是无效的上下文0x0messages--由于错误,上下文字面上设置为0x0(NULL)。我不知道为什么CGBitmapContextCreate有错误,但那是你应该开始调试的地方。检查所有输入参数,并将您对CGBitmapContextCreate的使用与Apple的一个示例进行比较,例如这一个

CGBitmapContextCreate returns NULL if there is an error creating the context, so that is the ultimate cause of the "invalid context 0x0" messages-- the context is literally set to 0x0 (NULL) because of an error. I don't know why CGBitmapContextCreate is having an error, but that's where you should start debugging. Check through all your input parameters, and compare your use of CGBitmapContextCreate with one of Apple's examples such as this one.

这篇关于CGContext&amp; CGBitmapContextCreateImage错误,如何修复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆