iOS - 在Objective C中从Web服务器加载图像

问题描述:

目前,我的目标是编写包含UIImageView的应用程序。加载或按下按钮后,来自Web服务器的图像将加载到视图中。

At the moment, I have a goal of writing an application that contains a UIImageView. Upon loading or pressing a button, an image from a web server will load into the view.

最简单的方法是什么?我假设使用此处描述的API? https://developer.apple.com/ library / mac / #documentation / Cocoa / Conceptual / URLLoadingSystem / Tasks / UsingNSURLConnection.html

What is the simplest way to do this? I'm assuming using the API described here? https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

同时,我会继续尝试这个独自工作。任何建议都会受到赞赏,特别是如果您之前已经编写了这样的代码。

In the mean time, I will continue to try to get this working on my own. Any advice would be appreciated, especially if you have written code like this before.

是的,这可以通过制作一个通过NSURLConnection请求。

Yes, this can be done by making a request through an NSURLConnection.

您可能有兴趣查看 SDWebImage 。此库包含一个类别,该类别向 UIImageView 添加一个方法,该方法从远程URL异步加载图像,如下所示:

You might be interested in checking out SDWebImage. This library includes a category that adds a method to UIImageView that makes asynchronously loading an image from a remote URL as simple as this:

[self.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];