方法一:
如下代码:
//将view转成图片并保存相册
@objc func saveBtnClick(){
let frame = view.frame
UIGraphicsBeginImageContext(frame.size)
view.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.loadImage(image: image!)
}
//保存图片
func loadImage(image:UIImage){
UIImageWriteToSavedPhotosAlbum(image, self, #selector(self.image(image:didFinishSavingWithError:contextInfo:)), nil)
}
//回调
@objc func image(image: UIImage, didFinishSavingWithError: NSError?,contextInfo: AnyObject)
{
if didFinishSavingWithError != nil
{
print("error!")
return
}
print("保存成功")
}
实测效果在IOS 12上可用,但是保存的质量不高。
方法二:
保存UIview
func UIImageWriteToSavedPhotosAlbum(_ image: UIImage, _ completionTarget: Any?, _ completionSelector: Selector?, _ contextInfo: UnsafeMutableRawPointer?)
最后在info.plist中加入权限:
UIimageView用法参考:http://www.hangge.com/blog/cache/detail_534.html