Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

forrestgrant

macrumors member
Original poster
Jun 24, 2008
34
0
Hi there,

I load an image like so:
Code:
UIImage *image = [UIImage imageNamed:@"myimage.png"];
But what if I want to load an image from the internet? Something to the effect of:
Code:
UIImage *image = [UIImage imageNamed:@"http://myurl.com/myimage.png"];

Any help is appreciated. Thanks!
 

sujithkrishnan

macrumors 6502
May 9, 2008
265
0
Bangalore
Hi there,

I load an image like so:
Code:
UIImage *image = [UIImage imageNamed:@"myimage.png"];
But what if I want to load an image from the internet? Something to the effect of:
Code:
UIImage *image = [UIImage imageNamed:@"http://myurl.com/myimage.png"];

Any help is appreciated. Thanks!

try
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:mad:"http://myurl.com/myimage.png"];
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
try
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:mad:"http://myurl.com/myimage.png"];

This will work (although you need two more ] before the ;) but will block the thread until the data is retrieved over the network. If you do this at startup of your application this may lead to a very long wait until your application is ready for use which your users are not going to like.

If this is the case you can use a NSURLConnection to dowload the data asynchronously and then create the image once the data is downloaded. This is obviously much more complicated...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.