使用 NSURLConnection 将图像加载到 UIImageView 中
嘿大家。我对 obj-c/xcode 的东西真的很陌生。我正在尝试加载 xib 文件的背景。为此,我使用 UIImageView 并用我从网上找到的图像填充它。问题是它真的很慢。感觉像是崩溃了,但事实并非如此。有人告诉我使用 NSURLConnection 来解决问题,但不知道如何解决。这是我之前使用的代码。
wallpaper.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://mysite.com/mobile/wallpaperR.asp?id=%i",customerID]]]];
我如何将上面的代码转换为 NSURLConnection 等效项?
Hey all. I'm really new at this obj-c/xcode stuff. I'm trying to load the background of my xib file. To do this i'm using a UIImageView and populating that with an image I found from the net. the problem with that is that it's REALLY slow. Feels like it's crashing but it's not. I was told to use an NSURLConnection to fix the problem but don't know how. Here's the code i was using previously.
wallpaper.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://mysite.com/mobile/wallpaperR.asp?id=%i",customerID]]]];
How do i translate the above code into the NSURLConnection equivalent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NSURLConnection
将在新线程中下载数据,因此您的应用程序会感觉更快。它很容易实现,但它确实需要您理解委托的概念。
我创建了一个有用的类来处理单独线程上的图像下载,我将与您共享代码,并添加注释让您知道发生了什么:
AsyncImage.h
AsyncImage.m
如果您需要帮助实现它,请发表评论我可以帮助你让它发挥作用,我记得当我开始开发时这对我来说也很痛苦。
NSURLConnection
will download the data in a new thread, so you app will feel much faster.It is pretty easy to implement but it does require you to understand the concept of delegation.
I have a useful class I created to handle image downloads on separate threads, I will share the code with you, with comments to let you know what is going on:
AsyncImage.h
AsyncImage.m
If you need help implementing it just leave a comment and I can help you get it working, I remember this used to be pain for me too when I started developing.
您需要在使用 Web 服务时对此进行解析。像这样,
并且需要实现解析器的委托方法作为示例。
然后在你的视图控制器中访问数据,从解析中你需要传递对象,使用数组或字典。
You need to do parsing for this as you are using the webservice.Like this
and need to implement parser's delegate method as an example.
Then in your Viewcontroller access the data,from parsing you need to pass objects,using array or dictionary.
有一个示例可能会有所帮助:
NSURLConnection 加载图像示例
There is an example which may help:
NSURLConnection loading image example