<错误>: CGContextSaveGState: 无效的上下文 0x0 错误

10

我遇到了有关从UIView准备UIImage的问题。有时候(但不总是),我会看到以下错误:

<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;
}
1个回答

4
错误信息 "<Error>: CGContextSaveGState: invalid context 0x0" 可能是因为您的CGContext为空,或者更有可能的情况是Context的大小为空(CGSizeZero)。

另外,尝试跟踪出现此错误的类,并查看是否有某个对象没有大小或框架。 - Juan Reyes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接