AFNetworking缓存图像是自动加载还是我们必须手动加载? [英] Does AFNetworking cache images load automatically or do we have to do it manually?

查看:90
本文介绍了AFNetworking缓存图像是自动加载还是我们必须手动加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AFNetworking从JSON Feed中加载图片。

I'm using AFNetworking to load images from JSON feed.

在这里,当用户第一次打开应用程序时,图像是从互联网加载的。没关系。

In here, first time when user opens the app, images load from the internet. It's fine.

但是当用户返回并从另一个视图再次访问时,使用该应用程序时,图像应该从缓存而不是从Internet加载。

But when user go back and come again from another view, while using the app, images should load from the cache, not from the internet.

我该怎么做?

- (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; dr

使用 -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 类现在有几种下载图像以及缓存图像的方法!

Import the UIImageView+AFNetworking header and viola! UIImageView class now has several methods to download images and also, to cache them!

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

In-memory Caching: You can use following methods for simple in-memory caching.

  • – setImageWithURL:
  • – setImageWithURL:placeholderImage:

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

Image will be retrieved from the in-memory cache, if the image exists, otherwise it will be downloaded from the URL and stored in the in-memory cache.

示例:

[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 设置图像的默认行为。

Note: If a success block is specified, it is the responsibility of the block to set the image of the image view before returning. If no success block is specified, the default behavior of setting the image with self.image = image is applied.

示例:

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

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

这篇关于AFNetworking缓存图像是自动加载还是我们必须手动加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆