【问题标题】:Load image array into tableview image view via JSON通过 JSON 将图像数组加载到 tableview 图像视图中
【发布时间】:2013-09-30 08:14:46
【问题描述】:

我想知道通常是否可以通过 URL 加载图像,该 URL 通过 JSON 解析到 UIImageView 中的 tableview 中。 p>

我在 UI 中尝试了这个,但没有成功,日志中只有两个图像。我不明白他为什么只给我看 2 个网址而不是 18 个。

   NSString *bildurl = [[arrayArtikelBild objectAtIndex:indexPath.row] objectForKey:@"bild"];
   NSLog(@"BildURL: %@", bildurl);
   UIImage *image = [UIImage imageWithContentsOfFile:bildurl];
   cell.artikelImage.image = image;

我知道我的解析代码可以正常工作,因为我已经让它解析和显示其他内容(我使用 NSJSONSerialization)。

提前致谢。

【问题讨论】:

    标签: objective-c json uitableview nsjsonserialization


    【解决方案1】:

    理论上你可以使用:

    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:bildurl]]];
    

    但这是非常不可取的,因为所有下载都将在主线程上完成 + 没有缓存。您需要异步加载图像以实现平滑滚动。我建议使用https://github.com/rs/SDWebImage 或 AFNetworking 的 UIImageView 扩展 (https://github.com/AFNetworking/AFNetworking)

    【讨论】:

    • 是的,我只是有相同的解决方案,性能很糟糕......会检查一下谢谢
    【解决方案2】:

    您可以使用 GCD (Grand Central Dispatch) 异步加载图像

    使用它肯定会提高加载tableview的性能。

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    
        dispatch_async(queue, ^{
           UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:bildurl]]];
    
            dispatch_sync(dispatch_get_main_queue(), ^{
                [cell.artikelImage setImage:image];
                [cell setNeedsLayout];
            });
        });
    

    【讨论】:

    • 我会避免使用这种精确的解决方案,因为 URL 不会与 indexPaths 相关联,因此会在快速滚动时混淆时间图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多