How to download an image from the web and display it in an UIImageView?
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
Would I need 开发者_如何学Goto mess around with NSURLConnection and the like, or is there a simple method that takes an web URL and downloads the image data automatically?
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;
精彩评论