为了账号安全,请及时绑定邮箱和手机立即绑定

如何将UIView转换为图像

如何将UIView转换为图像

繁星coding 2019-10-16 13:18:25
我想将UIView转换为图像并将其保存在我的应用程序中。有人可以告诉我如何拍摄视图的屏幕截图或将其转换为图像,以及将其保存在应用程序中(而不是相机胶卷)的最佳方法是什么?这是该视图的代码:var overView   = UIView(frame: CGRectMake(0, 0, self.view.frame.width/1.3, self.view.frame.height/1.3))overView.center = CGPointMake(CGRectGetMidX(self.view.bounds),CGRectGetMidY(self.view.bounds)-self.view.frame.height/16);overView.backgroundColor = UIColor.whiteColor()self.view.addSubview(overView)self.view.bringSubviewToFront(overView)
查看完整描述

3 回答

?
慕姐4208626

TA贡献1852条经验 获得超7个赞

扩展UIView应该可以解决问题。


extension UIView {


    // Using a function since `var image` might conflict with an existing variable

    // (like on `UIImageView`)

    func asImage() -> UIImage {

        let renderer = UIGraphicsImageRenderer(bounds: bounds)

        return renderer.image { rendererContext in

            layer.render(in: rendererContext.cgContext)

        }

    }

}

苹果不鼓励使用UIGraphicsBeginImageContextiOS 10并引入P3色域。UIGraphicsBeginImageContext仅sRGB和32位。他们介绍了新的UIGraphicsImageRendererAPI,该API完全基于颜色进行管理,基于块,具有PDF和图像的子类,并自动管理上下文生存期。请查看WWDC16会话205了解更多详细信息(图像渲染从11:50左右开始)


Edit1:为确保它能在所有设备上正常运行,请#available回退到iOS的早期版本:


extension UIView {


    // Using a function since `var image` might conflict with an existing variable

    // (like on `UIImageView`)

    func asImage() -> UIImage {

        if #available(iOS 10.0, *) {

            let renderer = UIGraphicsImageRenderer(bounds: bounds)

            return renderer.image { rendererContext in

                layer.render(in: rendererContext.cgContext)

            }

        } else {

            UIGraphicsBeginImageContext(self.frame.size)

            self.layer.render(in:UIGraphicsGetCurrentContext()!)

            let image = UIGraphicsGetImageFromCurrentImageContext()

            UIGraphicsEndImageContext()

            return UIImage(cgImage: image!.cgImage!)

        }

    }

}


查看完整回答
反对 回复 2019-10-16
?
胡说叔叔

TA贡献1804条经验 获得超8个赞

你可以使用扩展


extension UIImage {

    convenience init(view: UIView) {

        UIGraphicsBeginImageContext(view.frame.size)

        view.layer.render(in:UIGraphicsGetCurrentContext()!)

        let image = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        self.init(cgImage: image!.cgImage!)

    }

}

这是迅速的3/4版本:


extension UIImage {

    convenience init(view: UIView) {

        UIGraphicsBeginImageContext(view.frame.size)

        view.layer.render(in:UIGraphicsGetCurrentContext()!)

        let image = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        self.init(cgImage: image!.cgImage!)

    }

}


查看完整回答
反对 回复 2019-10-16
?
手掌心

TA贡献1942条经验 获得超3个赞

通过drawViewHierarchyInRect:afterScreenUpdates:将UIView转换为图像,这比renderInContext快很多倍


重要说明:请勿从viewDidLoad或viewWillAppear调用此函数,请确保在视图完全显示/加载后捕获视图


对象C


     UIGraphicsBeginImageContextWithOptions(myView.bounds.size, myView.opaque, 0.0f);

     [myView drawViewHierarchyInRect:myView.bounds afterScreenUpdates:NO];

     UIImage *snapshotImageFromMyView = UIGraphicsGetImageFromCurrentImageContext();

     UIGraphicsEndImageContext();


     myImageView.image =  snapshotImageFromMyView;

保存编辑的图像相册


     UIImageWriteToSavedPhotosAlbum(snapshotImageFromMyView, nil,nil, nil);

迅捷3/4


    UIGraphicsBeginImageContextWithOptions(myView.bounds.size, myView.isOpaque, 0.0)

    myView.drawHierarchy(in: myView.bounds, afterScreenUpdates: false)

    let snapshotImageFromMyView = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()

    print(snapshotImageFromMyView)

    myImageView.image = snapshotImageFromMyView

具有扩展功能的超简单通用化,iOS11,swift3 / 4


extension UIImage{

    convenience init(view: UIView) {


    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, 0.0)

    view.drawHierarchy(in: view.bounds, afterScreenUpdates: false)

    let image = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()

    self.init(cgImage: (image?.cgImage)!)


  }

}



Use : 

 //myView is completly loaded/visible , calling this code after only after viewDidAppear is call

 imgVV.image = UIImage.init(view: myView)

 // Simple image object

 let img =  UIImage.init(view: myView)


查看完整回答
反对 回复 2019-10-16
  • 3 回答
  • 0 关注
  • 753 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信