2010-12-22 29 views
10

我收到一些與UIView中的UIImage有關的問題。我有時(但並非總是)看到錯誤<Error>:CGContextSaveGState:無效的上下文0x0錯誤

<Error>: CGContextSaveGState: invalid context 0x0 

我正在使用iPhone SDK 4.0。我的代碼:

-(void)drawRect:(CGRect)rect 
{ 
    // Customized to draw some text 
} 


-(UIImage*) PrepareBackImage:(CGRect) aRect // aRect = (0,0,1800,1200) 
{ 
    UIImage* background; 

    background  = [self GetImageFromView:self toRect:self.frame]; 
    UIGraphicsBeginImageContext(aRect.size); 

    CGPoint backgroundPoint = CGPointMake(0,0); 
    [background drawAtPoint:backgroundPoint]; 

    UIImage* backImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return backImage; 
} 

- (UIImage *) GetImageFromView:(UIView *)aView toRect:(CGRect)aRect 
{ 
    CGSize pageSize = aRect.size; 
    UIGraphicsBeginImageContext(pageSize); 
    [aView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return image; 
} 

回答

4

的錯誤<Error>: CGContextSaveGState: invalid context 0x0意味着要麼你CGContext上爲空,或者在你的情況下,更合理的解釋,即上下文的大小爲空(CGSizeZero

+0

另外,儘量跟蹤類那是呈現這個錯誤,看看有些對象沒有大小或框架。 – 2014-12-05 14:38:31

相關問題