- ImageSaver class:
class ImageSaver: NSObject {
var successHandler: (() -> Void)?
var errorHandler: ((Error) -> Void)?
func writeToPhotoAlbum(image: UIImage) {
UIImageWriteToSavedPhotosAlbum(image, self, #selector(saveCompleted), nil)
}
@objc func saveCompleted(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
if let error = error {
errorHandler?(error)
} else {
successHandler?()
}
}
}
- button action to trigger the download of a png file from URL:
Button{
let imageSaver = ImageSaver()
imageSaver.successHandler = {
print("Success!")
}
imageSaver.errorHandler = {
print("Oops: \($0.localizedDescription)")
}
let imageRequestUrl = URL(string: lastImageUrl)!
// I'm sure that I can see the image url is valid and I can
// downloaded it from Google chrome successfully
print("imageRequestUrl:\(imageRequestUrl)")
let req = URLRequest(url: imageRequestUrl)
let task = URLSession.shared.dataTask(with: req) { d, res, err in
if let data = d, let image = UIImage(data: data) {
imageSaver.writeToPhotoAlbum(image: image)
}
}
task.resume()
}
- And this is the error:
findWriterForTypeAndAlternateType:119: unsupported file format 'org.webmproject.webp'
- More info
When I change the image URL to another one, like some other open-source png file, there's no error.... please help why it's so weird?