首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CGContext错误/警告

CGContext错误/警告
EN

Stack Overflow用户
提问于 2012-10-28 22:08:54
回答 2查看 5.3K关注 0票数 2

我有下面的代码来截图。我特别没有使用UIGraphicsBeginImageContext,因为之前我发现它使用了非常多的内存:

代码语言:javascript
复制
    CGSize size = activeView.frame.size;

    NSUInteger width = size.width;
    NSUInteger height = size.height;

    NSLog(@"BEFORE");

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    unsigned char *rawData = malloc(height * width * 4);
    memset(rawData,0,height * width * 4);

    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;
    CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

    CGRect bounds;
    bounds.origin = CGPointMake(0,0);
    bounds.size = size;

    CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), [self.topimage CGImage]);
    CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), [bottomimage CGImage]);

    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    UIImage *latest = [UIImage imageWithCGImage:imageRef scale:0.0 orientation:UIImageOrientationDownMirrored];

    [activeView.layer renderInContext:UIGraphicsGetCurrentContext()];

    CGImageRef imageRef2 = CGBitmapContextCreateImage(context);
    UIImage *latest2 = [UIImage imageWithCGImage:imageRef2 scale:0.0 orientation:UIImageOrientationDownMirrored];

    CGContextRelease(context);
    CGImageRelease(imageRef);
    CGImageRelease(imageRef2);

    NSLog(@"AFTER");

在“之前”和“之后”NSLogs之间,出现以下错误/警告:

代码语言:javascript
复制
<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextSetAlpha: invalid context 0x0
<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextSetFillColorWithColor: invalid context 0x0
<Error>: CGContextAddRect: invalid context 0x0
<Error>: CGContextDrawPath: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0
<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextGetBaseCTM: invalid context 0x0
<Error>: CGContextConcatCTM: invalid context 0x0
<Error>: CGContextSetBaseCTM: invalid context 0x0
<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextClipToRect: invalid context 0x0
<Error>: CGContextSetAlpha: invalid context 0x0
<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextSetFillColorWithColor: invalid context 0x0
<Error>: CGContextAddRect: invalid context 0x0
<Error>: CGContextDrawPath: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0
<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextConcatCTM: invalid context 0x0
<Error>: CGContextConcatCTM: invalid context 0x0
<Error>: CGContextClipToRect: invalid context 0x0
<Error>: CGContextDrawImage: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0
<Error>: CGContextSetBaseCTM: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0

为什么会发生这种情况?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-28 22:44:11

[activeView.layer renderInContext:UIGraphicsGetCurrentContext()];

这一行是problem...you没有“当前”上下文。请改用context

票数 6
EN

Stack Overflow用户

发布于 2014-01-04 19:41:07

这是iOS模拟器的一个问题。(苹果公司正在解决这个问题)

转到iOS模拟器->重置内容和设置

清理项目,然后重新运行它。我的问题解决了。一切都会恢复正常。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13109487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档