CGContextAddPath控制台错误?

时间:2012-05-02 03:09:54

标签: ios console cgcontext

在我的应用中,我在控制台中收到以下错误:

May  1 22:06:59 iPhone-4S app[93660] <Error>: CGContextAddPath: invalid context 0x0
May  1 22:06:59 iPhone-4S app[93660] <Error>: clip: invalid context 0x0

可以发生这种情况的唯一区域是使用以下两种方法之一来调整大小或向UIImage添加圆角边缘。以下是方法:

- (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize {
    // Create a graphics image context
    UIGraphicsBeginImageContext(newSize);

    // Tell the old image to draw in this new context, with the desired
    // new size
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

    // End the context
    UIGraphicsEndImageContext();

    // Return the new image.
    return newImage;
}

- (UIImage*)roundCorneredImage: (UIImage*)orig radius:(CGFloat) r {
    UIGraphicsBeginImageContextWithOptions(orig.size, NO, 0);
    [[UIBezierPath bezierPathWithRoundedRect:(CGRect){CGPointZero, orig.size} 
                                cornerRadius:r] addClip];
    [orig drawInRect:(CGRect){CGPointZero, orig.size}];
    UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return result;
}

这两个方法都是从UIViewControllers中调用的,我很肯定UIImage不是nil。

有没有理由发生这种情况?

谢谢!

1 个答案:

答案 0 :(得分:6)

确保上下文大小不为零。