使用 url 设置 UIImageView 图像

Set UIImageView image using a url(使用 url 设置 UIImageView 图像)

本文介绍了使用 url 设置 UIImageView 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以读取图像的 url 并将 UIImageView 设置为该 url 处的图像?

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

推荐答案

可以从 URL 加载 NSData 并将其转换为图像并将其粘贴在图像视图中,但这是一个非常糟糕的主意,因为它涉及在主线程上进行同步 URL 下载.这将锁定 UI,如果图像下载速度不快,可能会导致用户认为您的应用已崩溃.

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];

因此我的回答.可以通过 NSData 来做同样的事情,但这是个坏主意.相反,应该使用 NSURLConnection 异步下载图像,并且只有在完全下载后才能将其转换为 UIImage 并分配给图像视图.

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.

这篇关于使用 url 设置 UIImageView 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 url 设置 UIImageView 图像

基础教程推荐