我收到了与UIImage
中的UIView
相关的问题。我有时(但不总是)看到错误
<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;
}
答案 0 :(得分:4)
错误<Error>: CGContextSaveGState: invalid context 0x0
表示您的CGContext为NULL,或者在您的情况下更合理的解释,即Context的大小为空(CGSizeZero
)