This tip will show the steps to download and display an image from a remote resource. This is handy if you need to add an image as a subview, yet, the image is not part of your application bundle.
URL to Remote Image
We start by creating a URL to the remote resource:
NSURL *url = [NSURL URLWithString:
@"http://iphonedevelopertips.com/images/logo-iphone-dev-tips.png"];
Create UIImage from NSData
The next step is to build a UIImage using the data downloaded from the URL, which consists of an NSData object that holds the remote image contents:
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
Putting it Together
Here’s how to wrap it all together, adding the remote image as a subview to an existing view by creating a UIImageView from the above UIImage:
NSURL *url = [NSURL URLWithString:
@"http://iphonedevelopertips.com/images/logo-iphone-dev-tips.png"];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
[self.view addSubview:[[UIImageView alloc] initWithImage:image]];
436



被折叠的 条评论
为什么被折叠?



