2010-08-25 13 views
4

我已經在這裏好幾個小時了,甚至不知道如何調試這個錯誤。也許有一位知道發生了什麼的SO專家。CGContext&CGBitmapContextCreateImage錯誤,如何解決?

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

誰能告訴我這是怎麼回事的錯誤?提前致謝。

回答

10

如果創建上下文時發生錯誤,CGBitmapContextCreate將返回NULL,因此這是「無效上下文0x0」消息的最終原因 - 由於錯誤,上下文字面上設置爲0x0(NULL)。我不知道CGBitmapContextCreate爲什麼會出現錯誤,但這是您應該開始調試的地方。檢查所有輸入參數,並將CGBitmapContextCreate的使用與Apple的其中一個例子(如this one)進行比較。

8

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]; 
}