【问题标题】:invalid context 0x0, Creating UIImage from CALayer无效上下文 0x0,从 CALayer 创建 UIImage
【发布时间】: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

【问题讨论】:

    标签: ios calayer


    【解决方案1】:

    检查您的图层参数是否为nil。通常,如果大小的宽度 = 0 或高度 = 0(或两者兼有),则无法创建上下文。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-30
      • 2012-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多