繁体   English   中英

如何使用绝对URL设置UIImage?

[英]How do you set a UIImage using an absolute URL?

我正在尝试将tableview单元的UIImage设置为绝对URL,这是我已经拥有的代码:

NSString * imageURL = [[stories objectAtIndex:indexPath.row] objectForKey:@"images"];
NSURL *url = [NSURL URLWithString:imageURL];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
//set image
cell.imageView.image = img;

但是由于某种原因,它会返回null,就像提供的URL无效一样。 但是我知道不是这样,因为我可以通过将其中一个URL作为字符串文字键入imageURL指针来使其工作,并且我知道代码[[stories objectAtIndex:indexPath.row] objectForKey:@“ images”]返回正确的URL,因为我将输出打印到日志中,这很好。

有什么原因会导致我显然不在这里看到吗?

您可以使用NSURL和voila的absoluteURL方法!

NSURL *url = [[NSURL URLWithString:imageURL] absoluteURL];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM