2013-08-07 30 views
0

我想用這個post的代碼將CALayer轉換爲UIImage。在這裏複製。但是當我使用它時,我得到了很多錯誤。起初,我把它放在一個NSObject類中,我用它來存儲一個模型。但後來我在帖子中讀到,我想把它放在控制器,視圖或CALayer的類別中。我試圖把它放在控制器或CALayer中,但它仍然不起作用。無效的上下文0x0,從CALayer創建UIImage

- (UIImage *)imageFromLayer:(CALayer *)layer 
{ 

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) 
     UIGraphicsBeginImageContextWithOptions([layer frame].size, NO, [UIScreen mainScreen].scale); 
    else 
     UIGraphicsBeginImageContext([layer frame].size); 

    [layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    return outputImage; 
} 

錯誤:

<Error>: CGContextSetStrokeColorWithColor: invalid context 0x0 
<Error>: CGContextSetLineWidth: invalid context 0x0 
<Error>: CGContextMoveToPoint: invalid context 0x0 
<Error>: CGContextAddArc: invalid context 0x0 
<Error>: CGContextAddArc: invalid context 0x0 
<Error>: CGContextAddArc: invalid context 0x0 
<Error>: CGContextAddArc: invalid context 0x0 
<Error>: CGContextClosePath: invalid context 0x0 
<Error>: CGContextDrawPath: invalid context 0x0 
<Error>: CGContextRestoreGState: invalid context 0x0 
<Error>: CGContextRestoreGState: invalid context 0x0 
<Error>: CGContextSetBaseCTM: invalid context 0x0 
<Error>: CGContextRestoreGState: invalid context 0x0 
<Error>: CGContextEndTransparencyLayer: invalid context 0x0 
<Error>: CGContextRestoreGState: invalid context 0x0 
<Error>: CGContextSaveGState: invalid context 0x0 
<Error>: CGContextSetAlpha: invalid context 0x0 

回答

4

檢查您層參數nil。通常,如果大小的寬度= 0或高度= 0(或兩者都有),則無法創建上下文。

相關問題