views:

417

answers:

1

Hi,

I am trying to load image from a remote server on the UIImageView in my application. Is it possible to load image from a remote server. I am using the following code

id path = @"http://upload.wikimedia.org/wikipedia/commons/c/c7/Sholay-Main_Male_Cast.jpg";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];

I found this code sippnet on the net, but I dont understand how do I load this image on the ImageView.

Can anyone tell me how to add image on the UIImageView.

Thanx in advance...

+4  A: 

you got almost all the way there... now just

imageView.image = img;

note that using dataWithContentsOfURL will block your user interface while the image loads, which will work if you're loading small images at startup, but is generally a bad idea; look into NSURLConnection.

David Maymudes
Thanx... But can you tell me how to adjust the size of the image.
Atulkumar V. Jain
this is really a new question: you should check the check mark next to my answer and ask it separately. That said, what do you mean by "adjust the size of the image"? Do you really need to resize the UIImage, or do you just want to set the boundaries of your UIImageView? It may be that you just need to set your image view's contentMode property to UIViewContentModeScaleAspectFit to tell it to fit the image into the view rather than to resize the view...
David Maymudes
ok.. Thanx for ur suggestion...
Atulkumar V. Jain