Set UIImageView image using a url(使用 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 图像


- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01