我正在尝试使用此post的代码将CALayer转换为UIImage。在这里复制。但是当我使用它时,我遇到了很多错误。起初我把它放在一个NSObject类中,用于存储模型。但后来我在帖子中读到了我想把它放在CALayer的控制器,视图或类别中。我尝试将它放在控制器或CALayer中,它仍然无效。
- (UIImage *)imageFromLayer:(CALayer *)layer
{
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
UIGraphicsBeginImageContextWithOptions([layer frame].size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext([layer frame].size);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return outputImage;
}
ERROR:
<Error>: CGContextSetStrokeColorWithColor: invalid context 0x0
<Error>: CGContextSetLineWidth: invalid context 0x0
<Error>: CGContextMoveToPoint: invalid context 0x0
<Error>: CGContextAddArc: invalid context 0x0
<Error>: CGContextAddArc: invalid context 0x0
<Error>: CGContextAddArc: invalid context 0x0
<Error>: CGContextAddArc: invalid context 0x0
<Error>: CGContextClosePath: invalid context 0x0
<Error>: CGContextDrawPath: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0
<Error>: CGContextSetBaseCTM: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0
<Error>: CGContextEndTransparencyLayer: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0
<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextSetAlpha: invalid context 0x0
答案 0 :(得分:4)
检查您的图层参数是否为nil
。通常,如果size具有width = 0或height = 0(或两者),则无法创建上下文。