UIImageWriteToSavedPhotosAlbum save as PNG with transparency?

前端 未结 5 1820
轻奢々
轻奢々 2020-12-02 14:44

I\'m using UIImageWriteToSavedPhotosAlbum to save a UIImage to the user\'s photo album. The problem is that the image doesn\'t have transparency and is a JPG. I\'ve got the

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 15:33

    I created an extension of UIImage with safe unwrapping:

    Extension

    extension UIImage {
        func toPNG() -> UIImage? {
            guard let imageData = self.pngData() else {return nil}
            guard let imagePng = UIImage(data: imageData) else {return nil}
            return imagePng
        }
    }
    

    Usage:

    let image = //your UIImage
    if let pngImage = image.toPNG() {
         UIImageWriteToSavedPhotosAlbum(pngImage, nil, nil, nil)
    }
    

提交回复
热议问题