I new to AFNetworking and less experience in iOS development.
I use AFNetworking to download image from the internet, here is the code:
- (void)dowloadPoject {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
BOOL __block responseFromCache = YES; // yes by default
void (^requestSuccessBlock)(AFHTTPRequestOperation *operation, id responseObject) = ^(AFHTTPRequestOperation *operation, id responseObject) {
if (responseFromCache) {
// response was returned from cache
NSLog(@"RESPONSE FROM CACHE: %@", responseObject);
}
else {
// response was returned from the server, not from cache
NSLog(@"RESPONSE From Server: %@", responseObject);
}
UIImage *downloadedImage = [[UIImage alloc] init];
downloadedImage = responseObject;
// Add & Reload Data
[self.projectImage addObject:downloadedImage];
[self.projectname addObject:@"ABC"];
[self reloadComingSoonProject];
[self reloadNewReleaseProject];
};
void (^requestFailureBlock)(AFHTTPRequestOperation *operation, NSError *error) = ^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"ERROR: %@", error);
};
AFHTTPRequestOperation *operation = [manager GET:@"http://i359.photobucket.com/albums/oo34/SenaSLA/walls/Forward-Arrow-Button.png"
parameters:nil
success:requestSuccessBlock
failure:requestFailureBlock];
operation.responseSerializer = [AFImageResponseSerializer serializer];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
NSLog(@"bytesRead: %u, totalBytesRead: %lld, totalBytesExpectedToRead: %lld", bytesRead, totalBytesRead, totalBytesExpectedToRead);
[self.viewNavBar.progressbar setProgress:(totalBytesRead/totalBytesExpectedToRead) animated:YES];
}];
[operation setCacheResponseBlock:^NSCachedURLResponse *(NSURLConnection *connection, NSCachedURLResponse *cachedResponse) {
// this will be called whenever server returns status code 200, not 304
responseFromCache = NO;
return cachedResponse;
}];
}
I have been searching on internet and i still no get the right solution. i have tried this solution from rckoenes but this no work for me.
I have succeeded to cache an image like 4kb, but when i'm trying with image like 103kb it doesn't cache. Thank you.
You can try increasing the cache size. Last time I tested this, networking code wouldn't cache if the received data exceeded 5% of the total cache size (though, annoyingly, I have yet to see Apple clearly articulate the rules that are applied during caching).
Anyway, if you look at the app delegate in the sample AFNetworking project, that shows and example of how to specify the cache size:
NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:20 * 1024 * 1024 diskPath:nil];
[NSURLCache setSharedURLCache:cache];
It looks like the default RAM cache is only 0.5mb and the disk cache is 10mb. By increasing that RAM cache to 4mb, like shown above, or larger, and you should be able to cache your 103kb image (assuming that all the other criteria, such as header fields of the response and the like, permit it).