I have a problem using UIImageWriteToSavedPhotosAlbum
because I wanted to know at what point it started to download, because I have to download an image of 1.4M size and it takes a long time to download it, and I wanted to put a loader that indicate at user that is actually download the image. Any ideas?
I have to download my Image from a URL,
LoadingOverlay.shared.showOverlay(view: self.view) //NEVER EXECUTED, FREEZES BUTTON
let imagestring = Constants.baseURL + "photos/download/" + imagePhoto
if let url = URL(string: imagestring),
let data = try? Data(contentsOf: url),
let image = UIImage(data: data) {
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
@objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
LoadingOverlay.shared.hideOverlayView(view: self.view)
if let error = error {
// we got back an error!
let ac = UIAlertController(title: NSLocalizedString("Error", comment: ""), message: error.localizedDescription, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
} else {
let ac = UIAlertController(title: "Imagen guardada", message: "Tu imágen se guardó correctamente", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
}
I wanted to know how can I know where starts downloading or if there's another way to save photos to library.
The problem is not that you're saving to the photo album incorrectly. The problem is that you are downloading incorrectly. Never call Data(contentsOf: url)
as a way of downloading. Use URLSession. Now you will be downloading correctly, in the background, you can get progress notifications, and you will be called back when the data is downloaded and can proceed to save to the photo album.