How to create UIImageView with image from a link?

22,396

Solution 1

NSURL *url = [NSURL URLWithString:@"http://img.abc.com/noPhoto4530.gif"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

Solution 2

If you want to download the picture in the background, and then set it on the main thread, you can do it like this:

- (void)downloadPicture {

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

                NSURL *url = [NSURL URLWithString:@"http://img.abc.com/noPhoto4530.gif"];

                UIImage *image = [self getPicture:url];

                dispatch_async(dispatch_get_main_queue(), ^{

                    [self setPicture:image];

                });
            });
}

 - (UIImage *)getPicture:(NSURL *)pictureURL {

        NSData *data = [NSData dataWithContentsOfURL:pictureURL];
        UIImage *image = [UIImage imageWithData:data];

        return image;    
}

 - (void)setPicture:(UIImage *)image {

        UIImageView * imageView = [[UIImageView alloc] initWithFrame:
                               CGRectMake(kPictureX, kPictureY, image.size.height, image.size.width)];

        [imageView setImage:image];

        [self.view addSubview: imageView];

}

Solution 3

Here's a code snippet for those looking to use iOS 7's new suite of NSURLSession classes:

// Set NSURLSessionConfig to be a default session
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];

// Create session using config
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];

// Create URL
NSURL *url = [NSURL URLWithString:@"https://www.google.com/images/srpr/logo11w.png"];

// Create URL request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"GET";

// Create data task
NSURLSessionDataTask *getDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

    // Okay, now we have the image data (on a background thread)
    UIImage *image = [UIImage imageWithData:data];

    // We want to update our UI so we switch to the main thread
    dispatch_async(dispatch_get_main_queue(), ^{

        // Create image view using fetched image (or update an existing one)
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

        // Do whatever other UI updates are needed here on the main thread...
    });
}];

// Execute request
[getDataTask resume];

Solution 4

After downloading the image you need also to place it as a subview from a view, like so:

NSURL *url = [NSURL URLWithString:@"http://img.abc.com/noPhoto4530.gif"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];

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

Solution 5

Download image to a local path on your device then get a UIImage from imageWithContentsOfFile and use this to set the image in the UIImageView. Remember to cleanup your image file sometime.

Share:
22,396
asedra_le
Author by

asedra_le

Updated on May 03, 2020

Comments

  • asedra_le
    asedra_le almost 4 years

    How to create UIImageView with image from a link like this http://img.abc.com/noPhoto4530.gif?

  • Howard
    Howard about 10 years
    Works perfect! Cannot imagin that "dataWithContentsOfURL" is in its static method. I'm using Xamarin by the way.
  • Gökhan Çokkeçeci
    Gökhan Çokkeçeci about 10 years
    Can i timeout imageview ? when internet connection is slow application can be crashed so i wants to add timeout @nszombie
  • smirkingman
    smirkingman about 9 years
    The accepted answer is quite old and bad practice anyway. This is the correct answer (with proper error handling of course ;)).
  • MoDJ
    MoDJ about 8 years
    You should note that this is not a great solution because it blocks the main thread waiting for the download. An async loading approach for the data is much better.