开发者

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

How to download an image from the web and display it in an UIImageView?

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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜