gpt4 book ai didi

ios - SDWebImage无法从NSDictionary加载图像

转载 作者:行者123 更新时间:2023-12-01 19:52:00 25 4
gpt4 key购买 nike

我有一个UITableView,并且正在从RSS文件中加载一些带有关键字的文章:标题,URL和图像URL。我试图使用SDWebImage库在UITableViewCell中显示图像,并且当我通过从RSS下载的字典“加载”图像时,未加载图像。不过,图片的url已成功加载,并在加载之前打印。但是,当我复制打印的URL并将其粘贴到另一个字符串中以尝试加载该URL时,它将起作用。下面是我的代码。

NSString *urlofimage = [[feeds objectAtIndex:indexPath.row] objectForKey: @"image"];

NSLog(@"%@", urlofimage);

NSString *urlofimage1 = @"https://www.radioevros.gr/wp-content/uploads/2017/05/1495542572-74c82f60669c9db27cad113d22379c72.jpg";

[cell.imageview sd_setImageWithURL:[NSURL URLWithString:urlofimage] placeholderImage:[UIImage imageNamed:@"radio.png"]
options:SDWebImageRefreshCached];

因此,我的代码似乎没有错,因为我之前使用的是相同的代码,而且一直在工作。在上面的代码中,urlofimage1实际上是来自urlofimage的打印的url。如果我这样做:
[cell.imageview sd_setImageWithURL:[NSURL URLWithString:urlofimage1] placeholderImage:[UIImage imageNamed:@"radio.png"]
options:SDWebImageRefreshCached];

正在工作,但是如果我没有这样做。
[cell.imageview sd_setImageWithURL:[NSURL URLWithString:urlofimage] placeholderImage:[UIImage imageNamed:@"radio.png"]
options:SDWebImageRefreshCached];

这是在里面加载的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

有任何想法吗?谢谢。

最佳答案

我会怀疑您的词典中有一些无效字符,例如末尾有空格或难以发现的类似字符。

首先尝试对收到的字符串进行编码,然后再创建NSURL实例。

NSString *urlofimage = @"https://www.radioevros.gr/wp-content/uploads/2017/05/1495542572-74c82f60669c9db27cad113d22379c72.jpg ";
NSCharacterSet *set = [NSCharacterSet URLQueryAllowedCharacterSet];
NSString *endcodedString = [urlofimage stringByAddingPercentEncodingWithAllowedCharacters:set];
NSLog(@"%@", [NSURL URLWithString:endcodedString]);

编辑:

编码突出显示,URL末尾包含无效字符。让我们用以下代码摆脱它们:
 NSString * urlofimage = @"https://www.radioevros.gr/wp-content/uploads/2017/05/1495542572-74c82f60669c9db27cad113d22379c72.jpg  ";
NSString *trimmedString = [urlofimage stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"%@", [NSURL URLWithString:trimmedString]);

关于ios - SDWebImage无法从NSDictionary加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45252284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com