我想在 ImageView 中使用 URL 显示图像。我试过使用这个:
[cell.avatarImageView.image setImageWithURL:[NSURL URLWithString:@"http://www.innovaworks.com/wp-content/themes/innovaworks/images/home_person.png"]];
但它给了我这个警告:
UIImage may not respond to "setImageWithUrl"
并且图像未显示。
最佳答案
UIImage 上没有方法来设置带有 url 的图像。
有关如何根据 url 的数据内容设置图像的完整说明,请参阅此答案:https://stackoverflow.com/a/2782491/209867
要点是:
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.innovaworks.com/wp-content/themes/innovaworks/images/home_person.png"]]];
了解这是同步 UITableView
中的滚动性能会非常糟糕,您不应该使用它!我只是为了技术准确性和全面性而提供此答案。
您似乎是从某处复制此代码,有许多开源类别将从 url 异步加载图像。
一个这样的开源项目是 SDWebImage。 https://github.com/rs/SDWebImage这是一个广泛使用的项目,可让您轻松地异步从 url 加载 UIImage
。
关于ios - UIimage 可能无法响应 "setImageWithUrl"iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27126614/