我使用以下代码将UIView
保存为UIImage
:
extension UIView{
var screenshot: UIImage{
UIGraphicsBeginImageContext(self.bounds.size);
let context = UIGraphicsGetCurrentContext();
self.layer.renderInContext(context!)
let screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenShot
}
}
我使用以下功能保存了输出图像:
func saveImage (image: UIImage, path: String ) -> Bool{
let pngImageData = UIImagePNGRepresentation(image)
let result = pngImageData!.writeToFile(path, atomically: true)
return result
}
然后我在不同的视图中检索图像,如下所示:
func loadImageFromPath(path: String) -> UIImage? {
let image = UIImage(contentsOfFile: path)
if image == nil {
print("missing image at: \(path)")
}
return image
}
然而图片的质量降低了!任何人都可以帮助我如何保持UIImage
的最高质量?