如何将UIView转换为图像

我想将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)


繁星coding
浏览 754回答 3
3回答

慕姐4208626

扩展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!)        }    }}

胡说叔叔

你可以使用扩展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!)    }}

手掌心

通过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 / 4extension 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)
打开App,查看更多内容
随时随地看视频慕课网APP