21

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

5
  • FYI: you don't need to add <br> tags to posts Commented Mar 24, 2010 at 18:03
  • Your code look weird, stringwithString: is a class method, so basically I think [temp stringwithString:@"192.168.1.2x0/pic/LC.jpg"] is invalid. Did you try to just use dato=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"192.168.1.2x0/pic/LC.jpg"]] ? Commented Mar 24, 2010 at 18:06
  • assume that pic is an image in your class - is an assignation what you want to do? (pic = [pic initWithImage...]) Commented Mar 24, 2010 at 18:07
  • You seem to really misunderstand the basics of object creation in Objective-C. I would recommend reading Apple's introductory material such as The Objective-C Programming Language, particularly the chapter on object creation and initialization: developer.apple.com/Mac/library/documentation/Cocoa/Conceptual/… Commented Mar 24, 2010 at 18:20
  • Others have already answered most of this, but I am fairly certain that 192.168.1.2x0 is not a valid IP address. Commented Mar 24, 2010 at 18:28

5 Answers 5

82

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];
Sign up to request clarification or add additional context in comments.

4 Comments

thank you very much i didn't know how UIImageView worked! it was so simple! Thank you very much!
warning, dataWithContentsOfURL: is not async... you could block main thread here right?
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"]];
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.
6

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

1 Comment

For the record, that would still 'take ages', the UI just wouldn't lock up.
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.

Comments

3

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];

Comments

3

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;
     });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.