Does AFNetworking cache images load automatically or do we have to do it manually?

你说的曾经没有我的故事 提交于 2019-12-07 10:46:45

问题


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.

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.

How can I do that?

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

回答1:


tl;dr

Use -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];

Image Cache + AFNetworking

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:

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.

Example:

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

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

Disk Caching: You can use following method if you need to cache the image for longer than the user’s session. (tip: check NSURLRequestCachePolicy in NSURLRequest class)

  • -setImageWithURLRequest:placeholderImage:success:failure:

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.

Example:

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

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


来源:https://stackoverflow.com/questions/31770060/does-afnetworking-cache-images-load-automatically-or-do-we-have-to-do-it-manuall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!