Skip to content

Instantly share code, notes, and snippets.

@alexsanchezdev
Last active December 12, 2019 14:33
Show Gist options
  • Save alexsanchezdev/00d68edfcd2b6188620534191ed2d309 to your computer and use it in GitHub Desktop.
Save alexsanchezdev/00d68edfcd2b6188620534191ed2d309 to your computer and use it in GitHub Desktop.
URLSession download images example | Swift 3
if let image = imageURL {
let url = URL(string: image)
URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in
if error != nil {
print(error!)
return
}
DispatchQueue.main.async {
imageView.image = UIImage(data: data!)
}
}).resume()
}
@janeon
Copy link

janeon commented Jan 23, 2017

Why cell.profileImage instead of imageView? It does not seem to be a property of the UITableViewCell class in iOS10

@alexsanchezdev
Copy link
Author

@janeon thats true! It was an example for a custom cell

@AsselKashkenbayeva
Copy link

This is great thanks! but its not showing up my image but not printing an error.... Do you know why that could be? I don't have a custom cell so just have: }

            DispatchQueue.main.async {
                cell.imageView?.image = UIImage(data: data!)
            }

that should work right?

@nisha-1
Copy link

nisha-1 commented Jul 5, 2017

Here is some error there profileImageUrl contain nil value So image is not download in tableViewCell.Please can you give some suggestion about that..??

@albaqawi
Copy link

albaqawi commented Aug 20, 2017

Alex amazing share man! thank you

I just had to modify the code of the DispatchQueue to be the following to make it work. I am using Xcode 8.3.3.

DispatchQueue.main.async(execute: { () -> Void in
                    cell. profileImage?.image = UIImage(data: data!)
                })

Asselinka if you run this code you will get your default image and wait for a 2-5 secs and you will see the images you used load up in place of the default images as they are traveling back to device or simulator over internet from Firebase.

this is an excellent example to learn about multimedia network communication.

@alexsanchezdev
Copy link
Author

@nisha-1 maybe a late reply, but keep in mind this was a code for a custom cell. I already edit it to be useful in any case, didn't try it yet but should work.

@alexsanchezdev
Copy link
Author

@albaqawi thanks! Really appreciate the comment, will check it in new Xcode versions and make any change needed, didn't touch Xcode for a long time now hahaha

@alexsanchezdev
Copy link
Author

@asselinka default cells don't have image views in it, you can only access "title" and "subtitle" if I remember right... You have to make a custom cell with an imageView inside and then refer it in your code... Could be that the problem?

@kiritm-nimblechapps
Copy link

@guptshivu
Copy link

I am new in this.Can you please tell me how can you downloads 100 images asynchronously at a time using this?

@Aizaz-Abbasi
Copy link

how can we use this in collection view where is doesn't load all images some times repeat same image??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment