开发者

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

开发者 https://www.devze.com 2023-01-03 21:55 出处:网络
How to download an image from the web and display it in an UIImageView? For example, I have an URL like:

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

精彩评论

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

关注公众号