使用url设置UIImageView图像 [英] Set UIImageView image using a url

查看:106
本文介绍了使用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下载。这将锁定用户界面并可能导致用户认为如果图片下载速度非常快,您的应用已崩溃。

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图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆