PDA

View Full Version : UIImage




forrestgrant
Aug 26, 2008, 09:57 AM
Hi there,

I load an image like so:

UIImage *image = [UIImage imageNamed:@"myimage.png"];

But what if I want to load an image from the internet? Something to the effect of:

UIImage *image = [UIImage imageNamed:@"http://myurl.com/myimage.png"];


Any help is appreciated. Thanks!



sujithkrishnan
Aug 26, 2008, 10:02 AM
Hi there,

I load an image like so:

UIImage *image = [UIImage imageNamed:@"myimage.png"];

But what if I want to load an image from the internet? Something to the effect of:

UIImage *image = [UIImage imageNamed:@"http://myurl.com/myimage.png"];


Any help is appreciated. Thanks!

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

robbieduncan
Aug 26, 2008, 10:05 AM
try
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"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...