Loading image in image view from a URL, using the iPhone sdk?

21,936

Solution 1

You can convert your response from your URL to NSData and then use

NSURL *url = [NSURL URLWithString:@"<YOUR URL STRING HERE>"];

NSData *data = [[NSData alloc] initWithContentsOfURL:url];

UIImage *tmpImage = [[UIImage alloc] initWithData:data];

yourImageView.image = tmpImage;

Hope this helps.

EDIT:

Also to make it work in background you should call it using a seperate thread in an asynchronous manner so that it wont block your main thread as shown below.

  [NSThread detachNewThreadSelector:@selector(downloadAndLoadImage) toTarget:self withObject:nil];

Solution 2

For large amounts of data you should consider using NSURLConnection:

NSURLRequest * request = [[NSURLRequest alloc] initWithURL:url];

NSURLConnection * conn = [NSURLConnection connectionWithRequest:request delegate:myDelegate];

This way you can keep track of the data returned from the server with:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
}

You have stated that I have used below code with different url and it is giving image then it would be helpful if you provided the URL that you are trying to use.

Another idea to help track down the issue would be to UIWebView to verify that you are able to load the required image.

Solution 3

Please try the following code. i have tested it. its working fine.

//in .h file

 IBOutlet UIImageView *imgTest;

-(IBAction)buttonTapped:(id)sender;
-(void)LoadImage:(NSString *) irlString;
-(void)setImage:(NSData *) imgData;

//in .m file write the following code:

-(IBAction)buttonTapped:(id)sender
{
    [self performSelectorOnMainThread:@selector(LoadImage:) withObject:@"http://www.google.com/images/errors/logo_sm.gif" waitUntilDone:NO];
}

-(void)LoadImage:(NSString *) urlString
{
    NSURL *imgURL=[NSURL URLWithString:urlString];
    NSData *imgData=[NSData dataWithContentsOfURL:imgURL];
    [self performSelectorInBackground:@selector(setImage:) withObject:imgData];
}

-(void)setImage:(NSData *) imgData;
{
    imgTest.image=[UIImage imageWithData:imgData];
}
Share:
21,936
Rohit Dhawan
Author by

Rohit Dhawan

iPhone/iPad developer

Updated on July 09, 2022

Comments

  • Rohit Dhawan
    Rohit Dhawan almost 2 years

    I've gone through a lot of code from the web in an attempt to load an image view via URL. Unfortunately, none of the solutions I tried has worked for me and I get very strange errors. I am getting image data, but no image from this NSData. I used the below code with a sample URL and it works, giving me the represented image. However when I switch it to use my actual URL I run into problems because it seems like it is attempting to create/return an image which is too large in size. Here is my code:

        NSString *urlString = [[[_xmlDictionary objectForKey:@"response"] objectForKey:@"movie"] objectForKey:@"poster"];   
    
        NSArray *parts = [urlString componentsSeparatedByString:@"\""];
        urlString = [parts objectAtIndex:1];
    
        urlString = [urlString stringByAppendingFormat:@"/"];
        urlString = [urlString  
            stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
        //NSURL * imageURL = [NSURL URLWithString:urlString];
        NSLog(@"URL:%@",urlString);
        NSURL * imageURL = [NSURL URLWithString:urlString];
        NSData * imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
        UIImage * image = [[UIImage alloc] initWithData:imageData];
        m_imageView = [[UIImageView alloc] initWithImage:image];
        m_imageView.frame = CGRectMake(10, 10,300,300);
        [self.view addSubview:m_imageView];
        [imageData release];
        [image release];
    

    I'd appreciate any help, thanks in advance.

  • Andrew Barber
    Andrew Barber over 9 years
    Please consider including some information about your answer, rather than simply posting code. We try to provide not just 'fixes', but help people learn. You should explain what was wrong in the original code, what you did differently, and why your change(s) worked.
  • bickster
    bickster over 9 years
    If you use the async manner put the call to yourImageView.image = tmpImage; on the main thread. Ex. dispatch_async(dispatch_get_main_queue(), ^{ yourImageView.image = tmpImage; });
  • bickster
    bickster over 9 years
    Why are you downloading the image data on the main thread and updating the UI on a background thread? Shouldn't it be the other way around?