How to load a remote image URL into UIImageView – Swift 5

preview_player
Показать описание
UIImageView is designed to load only local images, but with a little work you can make it load remote images too. To get a basic solution, add an extension to UIImageView that downloads image data using a GCD background thread, then converts that to a UIImage, and loads it back into the image view on the main thread.

Рекомендации по теме
Комментарии
Автор

Questions? Comments? Tweet me @twostraws.

twostraws
Автор

Here's how to use it for those like me.
var pic: UIImageView
...
pic.load(url: <<your url>>)

cyrileo
Автор

Hi Paul! Nice, but how do I actually use this? I can't seem to figure it out.

bjornlau
Автор

can you please tell me how to show images from Marvel API?

rutvikhiranee
Автор

You need to show us, it working with a URL. I'm at least still not sure, do I just pot in the url into my UIImageView after adding the extension? And why not show us? Monkey see, monkey do, please? I'll remember and understand it way better if you are explicit with your examples. :)

SamuelHauptmannvanDam
Автор

Is it okay to use "Data(contentsOf: )"? Doesn't it cause performance issues?

abdou