【问题标题】:Store the image into cache for avoiding frequent loading from url in tableview将图像存储到缓存中,以避免从 tableview 中的 url 频繁加载
【发布时间】:2016-09-27 23:03:22
【问题描述】:
NSURL *url =[NSURL URLWithString:
@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"];

序列化部分在下面完成

(void)connectionDidFinishLoading{
    NSDictionary *allDict = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
    NSArray *loans = [allDict objectForKey:@"loans"];
    countryArray = [[NSMutableArray alloc] init];
    for (NSDictionary *diction in loans) {
        Country *countryObj = [[Country alloc]init];
        NSString *sector =  [diction objectForKey:@"sector"];
        countryObj.sector = sector;
        NSDictionary *imageID = [diction objectForKey:@"image"];
        NSString *img = [imageID objectForKey:@"id"];
        countryObj.idValue=img;
        NSString *activity =  [diction objectForKey:@"activity"];
        countryObj.activity = activity;
                NSString *name =  [diction objectForKey:@"name"];
        countryObj.name = name;
        NSDictionary *con = [diction objectForKey:@"location"];
        NSString *world = [con objectForKey:@"country"];
        countryObj.country= world;
        NSString *towname = [con objectForKey:@"town"];
        countryObj.down=towname;
        [countryArray addObject:countryObj];
    }
}

这包含图像和数据。我需要将图像存储到缓存中。

【问题讨论】:

  • 用户 SDWebimage 或 AFN 用于异步加载图像
  • 这是一个请求 API,您需要首先将请求发送到服务器,然后需要按照 nitin gohel 注释对响应进行 serlize 处理
  • 我已经完成了序列化并将其加载到 tableview 中。但事情是每次从 url 加载都需要时间。因为我比较新鲜,所以我不知道 SDWebimage/AFN
  • 你能展示你尝试过的代码吗
  • 请往上看,我已经添加了一些序列化代码

标签: ios objective-c ios7 ios5


【解决方案1】:

请使用SDWebImage,它包含您需要的一切。我已经使用这个库 2 年了。

按照该教程操作,该教程包含如何使用简单的一行代码在 UIImageView 中缓存图像,以及在后台预取所有图像的类,只需创建一个包含您要预取的所有图像的 NSArray。

希望有帮助!

【讨论】:

    猜你喜欢
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    相关资源
    最近更新 更多