【发布时间】:2013-10-04 15:56:39
【问题描述】:
我正在尝试使用来自此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>: 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
【问题讨论】: