Created
April 8, 2018 17:04
-
-
Save 3257/d6ed4831f9a6baea7eafb3230244662e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func downloadImage(from storageImagePath: String) { | |
// 1. Get a filePath to save the image at | |
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) | |
let documentsDirectory = paths[0] | |
let filePath = "file:\(documentsDirectory)/myimage.jpg" | |
// 2. Get the url of that file path | |
guard let fileURL = URL(string: filePath) else { return } | |
// 3. Start download of image and write it to the file url | |
storageDownloadTask = storageRef.child(storageImagePath).write(toFile: fileURL, completion: { (url, error) in | |
// 4. Check for error | |
if let error = error { | |
print("Error downloading:\(error)") | |
return | |
// 5. Get the url path of the image | |
} else if let imagePath = url?.path { | |
// 6. Update the unicornImageView image | |
self.unicornImageView.image = UIImage(contentsOfFile: imagePath) | |
} | |
}) | |
// 7. Finish download of image | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment