Download AFNetworking cache images automatically or do we need to do this manually?

I use AFNetworking to load images from a JSON channel.

Here, for the first time, when the user opens the application, downloading images from the Internet. This is normal.

But when the user returns and reappears from another view, when using the application, the images should be downloaded from the cache, and not from the Internet.

How can i do this?

- (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; } 
+4
source share
1 answer

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 and viola header! The UIImageView class now has several methods for loading images, as well as caching them!

In-memory caching:. You can use the following methods for easy caching in memory.

The image will be extracted from the cache in memory, if the image exists, otherwise it will be loaded from the URL and stored in the cache in memory.

Example:

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

Disk cache You can use the following method if you need to cache an image longer than a user session. (hint: check NSURLRequestCachePolicy in class NSURLRequest )

Note. If a success block is specified, the block must set the image of the image before returning. If no success block is specified, the default behavior is used to set the image using self.image = image .

Example:

  NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:imageURL] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60]; [imageView setImageWithURLRequest:imageRequest placeholderImage:[UIImage imageNamed:@"placeholder.png"] success:nil failure:nil]; 
+2
source

All Articles