我遇到了一个奇怪的问题,如果能得到任何人的帮助,我们将不胜感激。
我的问题是我只能通过 Web 服务从服务器获取 ex: abc.png
的图像名称。
现在该图像需要附加静态 URL,然后它将用于显示到 UIImageView
。
我用过这段代码:
for (int i = 0; i<[[myDic valueForKey:@"posts"] count]; i++)
{
NSData *bgImageData = nil;
if ([[[[myDic valueForKey:@"posts"] objectAtIndex:i] valueForKey:@"post"] valueForKey:@"video_image"] == [NSNull null])
{
bgImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://drupal.org/files/issues/no-pre.png"]];
}
else
{
bgImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://sitebildz.com/application/document/video/%@",[[[[myDic valueForKey:@"posts"] objectAtIndex:i] valueForKey:@"post"] valueForKey:@"video_image"]]]];
}
[arrVideoImage addObject:bgImageData];
}
在表格中显示:
cell.imageView.image = [UIImage imageWithData:[arrVideoImage objectAtIndex:indexPath.row]];
但不知道为什么它不起作用。
谢谢。
最佳答案
正如 The Tiger 所说,您最好的选择是使用外部库,例如 SDWebImage它可以处理在线图像的下载、缓存和显示。 下面是一个使用这个框架的例子:
[imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
您还可以在图像加载完成后使用 block 来插入逻辑:
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {... completion code here ...}];
关于iphone - 将来自服务器的图像显示到 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16562327/