Search code examples
iosuiimagecell

UIimage may not respond to "setImageWithUrl" iOS


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.


Solution

  • 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.