ios – AFNetworking缓存图像会自动加载还是我们必须手动加载?

我正在使用AFNetworking从
JSON提要加载图像.

在这里,用户第一次打开应用程序时,图像会从互联网上加载.没关系.

但是当用户返回并从另一个视图再次使用时,图像应该从缓存加载,而不是从互联网加载.

我怎样才能做到这一点?

- (void)loadDetailData
{
    detailPost  = nil;
    NSString *detailUrl = [NSString stringWithFormat:@"%@", self.Details.firsturl];
    AFHTTPRequestOperationManager *detailManager = [AFHTTPRequestOperationManager manager];
    [detailManager GET:detailUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        detailPosts = (NSDictionary *)responseObject;
        detailPost = [NSMutableArray array];
        NSArray *result = [detailPosts objectForKey:@"posts"];
        for (NSDictionary *all in result)
        {
            Categories *newCategory = [Categories new];
            NSDictionary *thumbnail_images = [all objectForKey:@"thumbnail_images"];
            NSDictionary *mediumImages = [thumbnail_images objectForKey:@"medium"];
            newCategory.detailimageUrl = [mediumImages objectForKey:@"url"];

            newCategory.title = [all objectForKey:@"title"];
            newCategory.mogowebUrl = [all objectForKey:@"url"];
//            NSLog(@"%@", newCategory.title);
            [detailPost addObject:newCategory];
            [self.maintableView reloadData];
        }


    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [detailPost count];;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier"];
    Categories *details = [detailPost objectAtIndex:indexPath.row];


    cell.detailTitle.text = details.title;
    NSString *imageurl = [NSString stringWithFormat:@"%@", details.detailimageUrl];
    NSURL *imgurl = [NSURL URLWithString:imageurl];
    [cell.detailImageView setImageWithURL:imgurl placeholderImage:nil];





//    [cell addSubview:cell.subView];

    cell.layer.masksToBounds = NO;
    cell.layer.cornerRadius = 5;
    cell.layer.borderColor = [UIColor blackColor].CGColor;
    cell.layer.shadowOffset = CGSizeMake(0, 1);
    cell.layer.shadowRadius = 2.0;
    cell.layer.shadowColor = [UIColor lightGrayColor].CGColor;


    return cell;
}

最佳答案 TL;博士

使用-setImageWithURLRequest:placeholderImage:success:failure:

  NSURLRequest *imageRequest =
      [NSURLRequest requestWithURL:[NSURL URLWithString:imgurl]
                       cachePolicy:NSURLRequestReturnCacheDataElseLoad
                   timeoutInterval:60];

  [cell.detailImageView setImageWithURLRequest:imageRequest
                              placeholderImage:nil
                                       success:nil
                                       failure:nil];

图像缓存AFNetworking

导入UIImageView AFNetworking标题和中提琴! UIImageView类现在有几种方法可以下载图像,也可以缓存它们!

内存中缓存:您可以使用以下方法进行简单的内存缓存.

> – setImageWithURL:
> – setImageWithURL:placeholderImage:

如果图像存在,将从内存缓存中检索图像,否则将从URL下载并存储在内存缓存中.

例:

[imageView setImageWithURL:[NSURL URLWithString:imageURL]];

//here, placeholder image is set immediately.
[imageView setImageWithURL:[NSURL URLWithString:imageURL]
          placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

磁盘缓存:如果需要将图像缓存的时间超过用户的会话,则可以使用以下方法. (提示:检查NSURLRequest类中的NSURLRequestCachePolicy)

> -setImageWithURLRequest:placeholderImage:success:failure:

注意:如果指定了成功块,则块的责任是在返回之前设置图像视图的图像.如果未指定成功块,则应用使用self.image = image设置图像的默认行为.

例:

  NSURLRequest *imageRequest =
      [NSURLRequest requestWithURL:[NSURL URLWithString:imageURL]
                       cachePolicy:NSURLRequestReturnCacheDataElseLoad
                   timeoutInterval:60];

  [imageView setImageWithURLRequest:imageRequest
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                            success:nil
                            failure:nil];
点赞