Xcode iPhone Programming: Loading a jpg into a UIImageView from URL

72,970

Solution 1

It should be:

NSURL * imageURL = [NSURL URLWithString:@"http://192.168.1.2x0/pic/LC.jpg"];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image = [UIImage imageWithData:imageData];

Once you have the image variable, you can throw it into a UIImageView via it's image property, ie:

myImageView.image = image;
//OR
[myImageView setImage:image];

If you need to escape special characters in your original string, you can do:

NSString * urlString = [@"http://192.168.1.2x0/pic/LC.jpg" stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL * imageURL = [NSURL URLWithString:urlString];
....

If you're creating your UIImageView programmatically, you do:

UIImageView * myImageView = [[UIImageView alloc] initWithImage:image];
[someOtherView addSubview:myImageView];
[myImageView release];

Solution 2

And because loading image from URL takes ages, it would be better to load it asynchronously. The following guide made it stupid-simple for me:

http://www.switchonthecode.com/tutorials/loading-images-asynchronously-on-iphone-using-nsinvocationoperation

Solution 3

Try this

NSURL *url = [NSURL URLWithString:@"http://192.168.1.2x0/pic/LC.jpg"];
 NSData *data = [NSData dataWithContentsOfURL:url];
UIImageView *subview = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,320.0f, 460.0f)];
[subview setImage:[UIImage imageWithData:data]]; 
[cell addSubview:subview];
[subview release];

All the Best.

Solution 4

You should load the image in the background or the view will freeze. Try this:

UIImage *img = [[UIImage alloc] init];

dispatch_async(dispatch_get_global_queue(0,0), ^{

    NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:@"http://www.test.com/test.png"];
    img = [UIImage imageWithData: data];

     dispatch_async(dispatch_get_main_queue(), ^{
        //PUT THE img INTO THE UIImageView (imgView for example)
        imgView.image = img;
     });
});

Solution 5

This is the actual code.

UIImageView *myview=[[UIImageView alloc]init];

    myview.frame = CGRectMake(0, 0, 320, 480);

    NSURL *imgURL=[[NSURL alloc]initWithString:@"http://soccerlens.com/files/2011/03/chelsea-1112-home.png"];

    NSData *imgdata=[[NSData alloc]initWithContentsOfURL:imgURL];

    UIImage *image=[[UIImage alloc]initWithData:imgdata];

    myview.image=image;

    [self.view addSubview:myview];
Share:
72,970
Antonio Murgia
Author by

Antonio Murgia

Updated on July 31, 2022

Comments

  • Antonio Murgia
    Antonio Murgia almost 2 years

    My app has to load an image from a http server and displaying it into an UIImageView
    How can i do that??
    I tried this:

    NSString *temp = [NSString alloc];
    [temp stringwithString:@"http://192.168.1.2x0/pic/LC.jpg"]
    temp=[(NSString *)CFURLCreateStringByAddingPercentEscapes(
        nil,
        (CFStringRef)temp,                     
        NULL,
        NULL,
        kCFStringEncodingUTF8)
    autorelease];
    
    
    NSData *dato = [NSData alloc];
     dato=[NSData dataWithContentsOfURL:[NSURL URLWithString:temp]];
     pic = [pic initWithImage:[UIImage imageWithData:dato]];
    

    This code is in viewdidload of the view but nothing is displayed! The server is working because i can load xml files from it. but i can't display that image!
    I need to load the image programmatically because it has to change depending on the parameter passed! Thank you in advance. Antonio

  • Antonio Murgia
    Antonio Murgia about 14 years
    thank you very much i didn't know how UIImageView worked! it was so simple! Thank you very much!
  • Goles
    Goles over 11 years
    warning, dataWithContentsOfURL: is not async... you could block main thread here right?
  • Tim Wachter
    Tim Wachter over 10 years
    For the record, that would still 'take ages', the UI just wouldn't lock up.
  • Joe Fratianni
    Joe Fratianni about 10 years
    I have found images loaded from a url appear blurry compared to the same image loaded locally using [self.imageView setImage: [UIImage imageNamed:@"AKL.JPG"]];
  • ThePunisher
    ThePunisher over 9 years
    This is not as a Background task and makes the View Freeze. You should use blocks or NSThread to load the image in the background.