Skip to content

Instantly share code, notes, and snippets.

@3257
Created April 8, 2018 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3257/d6ed4831f9a6baea7eafb3230244662e to your computer and use it in GitHub Desktop.
Save 3257/d6ed4831f9a6baea7eafb3230244662e to your computer and use it in GitHub Desktop.
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