views:

34

answers:

1

How to download an image from the web and display it in an UIImageView? For example, I have an URL like: http://sstatic.net/so/apple-touch-icon.png

alt text

Would I need to mess around with NSURLConnection and the like, or is there a simple method that takes an web URL and downloads the image data automatically?

+1  A: 

You can use NSData's dataWithContentsOfURL: class method to download the image data from the server, then just pass that data to UIImage's imageWithData:class method.

Here's an example:

NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://sstatic.net/so/apple-touch-icon.png"]];

UIImage *downloadedImage = [UIImage imageWithData:imageData];

myImageView.image = downloadedImage;
Jacob Relkin
is that method working in the background, or does it block the current thread?
dontWatchMyProfile
It blocks. If you need asynchronous requests, use `NSURLConnection`
Jacob Relkin
And how does it handle connection problem ? Returns nil ?
VdesmedT