0

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

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 不为零。

发生这种情况有什么原因吗?

谢谢!

4

1 回答 1

6

确保上下文大小不为零。

于 2012-05-02T05:49:07.707 回答