Mohammad Shaker
Mohammad Shaker

Reputation: 185

ios - download and view image from web url xcode

i need download image from url and load it into UIIamge view

first i get image from URL , but data return with nil i dont know why ?

 -(UIImage *) getImageFromURL:(NSString *)fileURL {
        UIImage * result;
    // data here return with nil 
        NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
        result = [UIImage imageWithData:data];

        return result;
    }

Upvotes: 1

Views: 4591

Answers (2)

dmerlea
dmerlea

Reputation: 946

You can use AFNetworking

And simply use: - (void)setImageWithURL:(NSURL *)url;

Or something more complex:

UIImageView image;
NSString *urlstring = [NSString stringWithFormat:@"http://url/"];
NSURL *url = [NSURL URLWithString:urlstring];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[image setImageWithURLRequest:request
              placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                       success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
                            // Succes on loading image
                       }
                       failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
                            // Fail to load image
                       }];

Upvotes: 2

Rumin
Rumin

Reputation: 3790

You can set your image from URL link like this:

  NSString *your_url = @"http://your_link/";
  image_View.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL   URLWithString:your_url]]]; 

Also refer :

1) question 1

2) question 2

3) question 3

Upvotes: 2

Related Questions