Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS中CALayer的UIImage_Ios_Objective C_Uiimageview_Uiimage_Calayer - Fatal编程技术网

iOS中CALayer的UIImage

iOS中CALayer的UIImage,ios,objective-c,uiimageview,uiimage,calayer,Ios,Objective C,Uiimageview,Uiimage,Calayer,在我的应用程序中,我创建了一个CALayer(有几个子层-CALayer由作为子层添加的形状组成) 我正在尝试创建一个UIImage,我可以上传到服务器上(我有这方面的代码)。 但是,我不知道如何将CALayer添加到UIImage 这可能吗?听起来像是要将层渲染成UIImage。在这种情况下,下面的方法应该可以做到这一点。只需将其添加到视图或控制器类,或在CALayer上创建一个类别 Obj-C - (UIImage *)imageFromLayer:(CALayer *)layer {

在我的应用程序中,我创建了一个
CALayer
(有几个子层-CALayer由作为子层添加的形状组成)

我正在尝试创建一个
UIImage
,我可以上传到服务器上(我有这方面的代码)。 但是,我不知道如何将
CALayer
添加到
UIImage


这可能吗?

听起来像是要将层渲染成UIImage。在这种情况下,下面的方法应该可以做到这一点。只需将其添加到视图或控制器类,或在CALayer上创建一个类别

Obj-C

- (UIImage *)imageFromLayer:(CALayer *)layer
{
  UIGraphicsBeginImageContextWithOptions(layer.frame.size, NO, 0);

  [layer renderInContext:UIGraphicsGetCurrentContext()];
  UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext();

  return outputImage;
}
Swift

func imageFromLayer(layer:CALayer) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(layer.frame.size, layer.isOpaque, 0)
    layer.render(in: UIGraphicsGetCurrentContext()!)
    let outputImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return outputImage!
}

托德的答案是正确的,但是对于视网膜屏幕,应该有一点不同:

- (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;
}

我已为此创建了Swift扩展:

extension UIImage {
    class func imageWithLayer(layer: CALayer) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(layer.bounds.size, layer.opaque, 0.0)
        layer.renderInContext(UIGraphicsGetCurrentContext()!)
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return img
    }
}
用法:

var gradient = CAGradientLayer()
gradient.colors = [UIColor.redColor().CGColor, UIColor.blueColor().CGColor]
gradient.frame = CGRect(x: 0, y: 0, width: 200, height: 200)

let image = UIImage.imageWithLayer(gradient)

Swift 3版本,在检查上下文时有一点错误

extension UIImage {
  class func image(from layer: CALayer) -> UIImage? {
    UIGraphicsBeginImageContextWithOptions(layer.bounds.size, 
  layer.isOpaque, UIScreen.main.scale)

    defer { UIGraphicsEndImageContext() }

    // Don't proceed unless we have context
    guard let context = UIGraphicsGetCurrentContext() else {
      return nil
    }

    layer.render(in: context)
    return UIGraphicsGetImageFromCurrentImageContext()
  }
}

完美的解决方案…谢谢:)交换
UIGraphicsBeginImageContextWithOptions(layer.frame.size,是,0)我建议使用
UIGraphicsBeginImageContextWithOptions(layer.frame.size,layer.不透明,0)如何更新此选项以将CALayer的一部分(给定层中的一个矩形)渲染为图像而不是整个帧?您是一个救生员。为
UIGraphicsBeginImageContextWithOptions()指定
0
scale
参数默认使用
UIScreen.mainScreen.scale
。(根据文档,“应用于位图的比例因子。如果您指定的值为0.0,则比例因子设置为设备主屏幕的比例因子。”)您的回答在功能上没有@Todd Yandell的改进。您可以使用
defer
来处理
UIGraphicsSendImageContext()
很棒的建议。更新为用户延迟。