Set UIImageView image using a url

后端 未结 10 692
隐瞒了意图╮
隐瞒了意图╮ 2020-12-08 02:24

Is it possible to read a url to an image and set a UIImageView to the image at this url?

10条回答
  •  囚心锁ツ
    2020-12-08 02:55

    It's possible to load an NSData from a URL and convert that to an image and stick that in the image view, but this is an extremely bad idea because it involves doing a synchronous URL download on the main thread. This will lock up the UI and possibly cause the user to think your app has crashed if the image doesn't download extremely fast.

    Edit: To clarify, the original question looks to me like the poster wants to say something along the lines of

    imageView.image = [UIImage imageWithContentsOfURL:theURL];
    

    Hence my answer. It's possible to do the equivalent by going through NSData, but it's a bad idea. Instead, the image should be downloaded asynchronously using NSURLConnection, and only once it's fully downloaded should it be converted into a UIImage and assigned to the image view.

提交回复
热议问题