在Swift中以最高质量保存UIImage

时间:2016-05-11 14:37:21

标签: ios swift uiview uiimageview uiimage

我使用以下代码将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的最高质量?

0 个答案:

没有答案