iphone - CGContext 错误/警告

标签 iphone objective-c ios cgcontext

我有以下代码来截图。我特别没有使用 UIGraphicsBeginImageContext,因为在我发现这使用了大量内存之前:

    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 之间,出现以下错误/警告:

<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

为什么会发生这种情况?

最佳答案

[activeView.layer renderInContext:UIGraphicsGetCurrentContext()];

这一行是问题所在...您没有“当前”上下文。请改用上下文

关于iphone - CGContext 错误/警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13109487/

相关文章:

ios - 如何在ios中异步下载图片?

iphone - 将 COCOnut 添加到 XCode 中的现有项目

iphone - 如何在 itunesconnect 中重命名您的应用程序?

objective-c - 引用计数的不那么明显的减少

ios - 如何在 iOS 7 中获取设备或 iPad 的唯一编号?

objective-c - 获取CCSprite在cocos2d中的绝对位置

ios - 丰富的本地通知

iphone - 有哪些 iPhone 推送通知服务? (托管服务,而不是 APNS)

iphone - 我应该在 View Controller 中的哪里调用方法?

objective-c - 如何在 iOS 上试验私有(private) API?