CGContext & CGBitmapContextCreateImage 错误,如何修复?

发布于 2024-09-16 11:11:40 字数 2192 浏览 7 评论 0原文

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

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

这些

是我调用此方法后出现的错误

<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

有人可以告诉我发生了什么事吗?提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

肩上的翅膀 2024-09-23 11:11:40

如果创建上下文时出现错误,CGBitmapContextCreate 将返回 NULL,这就是“无效上下文 0x0”消息的最终原因——由于错误,上下文实际上被设置为 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.

长途伴 2024-09-23 11:11:40

drawRect中编写绘图代码。当您想要绘制时,调用[self setNeedsDisplay],它会调用您的drawRect实现。如果您从视图控制器内进行绘制,请改为调用 [self.view setNeedsDisplay]。例如:

-(void)drawRect:(CGRect)rect {
    CGRect currentRect = CGRectMake(50,50,20,20);
    UIColor *currentColor = [UIColor redColor];

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, 2.0);
    CGContextSetStrokeColorWithColor(context, currentColor.CGColor);

    CGContextSetFillColorWithColor(context, currentColor.CGColor);  
    CGContextAddEllipseInRect(context, currentRect);
    CGContextDrawPath(context, kCGPathFillStroke);
}   

- (void)nowIWantToDraw {
  [self.view setNeedsDisplay];
}

Write the drawing code in drawRect. When you want to draw, call [self setNeedsDisplay], which calls your implementation of drawRect. If you're drawing from within a view controller, call [self.view setNeedsDisplay] instead. For example:

-(void)drawRect:(CGRect)rect {
    CGRect currentRect = CGRectMake(50,50,20,20);
    UIColor *currentColor = [UIColor redColor];

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, 2.0);
    CGContextSetStrokeColorWithColor(context, currentColor.CGColor);

    CGContextSetFillColorWithColor(context, currentColor.CGColor);  
    CGContextAddEllipseInRect(context, currentRect);
    CGContextDrawPath(context, kCGPathFillStroke);
}   

- (void)nowIWantToDraw {
  [self.view setNeedsDisplay];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文