AFNetworkingcaching图像是否自动加载,还是需要手动进行?

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

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

但是,当用户从另一个视图回来,再次使用应用程序时,图像应该从caching,而不是从互联网加载。

我怎样才能做到这一点?

- (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:成功:失败:

  NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:imgurl] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60]; [cell.detailImageView setImageWithURLRequest:imageRequest placeholderImage:nil success:nil failure:nil]; 

图像caching+ AFNetworking

导入UIImageView + AFNetworking标题和中提琴! UIImageView类现在有几种方法来下载图像,并caching它们!

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

  • – setImageWithURL:
  • – setImageWithURL:placeholderImage:

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

例:

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

磁盘caching:如果您需要caching图像的时间超过用户的会话,则可以使用以下方法。 (提示:在NSURLRequest类中检查NSURLRequestCachePolicy

  • -setImageWithURLRequest:placeholderImage:成功:失败:

注意:如果指定了成功块,则在返回之前设置图像视图的图像是块的责任。 如果未指定成功块,则应用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];