2012-10-28 189 views
2

我有以下代碼來獲取屏幕截圖。我特別不使用UIGraphicsBeginImageContext,因爲之前我發現這個使用非常多的內存:CGContext錯誤/警告

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 

這是怎麼發生的?

+1

你沒有設置圖形上下文,你想知道爲什麼它告訴你,你有一個無效的上下文? – user1118321

+8

非常粗魯... –

回答

6

[activeView.layer renderInContext:UIGraphicsGetCurrentContext()];

此行是問題......你有沒有「當前」的環境。改爲使用context

-1

這是與iOS simuator問題。(蘋果的這個bug的工作)

轉到iOS的Simulator->重置內容和設置

清理項目,然後再次運行它。我的問題解決了。一切都會恢復正常。