0

I want to display an image using a URL in an image view. I tried using this:

[cell.avatarImageView.image setImageWithURL:[NSURL URLWithString:@"http://www.innovaworks.com/wp-content/themes/innovaworks/images/home_person.png"]];

but it gives me this warning :

UIImage may not respond to "setImageWithUrl"

and the image not shown.

Andy Obusek
  • 12,614
  • 4
  • 41
  • 62
arbi mahdi
  • 64
  • 2
  • 9

3 Answers3

2

There is no method on UIImage to set the image with a url.

See this answer for a full description of how to set an image from the data contents of a url:https://stackoverflow.com/a/2782491/209867

The gist is:

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.innovaworks.com/wp-content/themes/innovaworks/images/home_person.png"]]];

Understand that this is synchronous The scroll performance within a UITableView will be horrendous, and you should not use this! I only provide this answer for technical accuracy and comprehensiveness.

As it looks like you copied this code from somewhere, there are many open source categories that will asynchronously load images from a url.

One such open source project is SDWebImage. https://github.com/rs/SDWebImage It's a widely used project that easily allows you to asynchronously load a UIImage from a url.

Community
  • 1
  • 1
Andy Obusek
  • 12,614
  • 4
  • 41
  • 62
0

-setImageWithURL: is not an UIImage method. You want to fetch the image data first, using NSURLRequest, NSURLSession, etc. (documentation). Then allocate an UIImage and use the initWithData: constructor. Then use the image view's setImage: method to set it.

Thomas Deniau
  • 2,488
  • 1
  • 15
  • 15
0

Try

 [cell.avatarImageView setImageWithURL:[NSURL URLWithString:@"http://www.innovaworks.com/wp-content/themes/innovaworks/images/home_person.png"]];

Or

cell.avatarImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.innovaworks.com/wp-content/themes/innovaworks/images/home_person.png"]]];
Nikita Khandelwal
  • 1,741
  • 16
  • 25