There are many third parties libraries for loading a network image and then store it into a disk and/or memory.
However it is very easy to implement it using simple NSURLSession API call.
here is the code:
NSURLCache *myCache = [[NSURLCache alloc] initWithMemoryCapacity: 16384 diskCapacity: 268435456 diskPath: cachePath]; // these numbers are only for the usage example.
defaultConfigObject.URLCache = myCache;
defaultConfigObject.requestCachePolicy = NSURLRequestUseProtocolCachePolicy;
_session = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate:self delegateQueue: [NSOperationQueue mainQueue]];
_dataTask = [_session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
if (!error){
UIImage* theImage = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
self.image = theImage;
});
}
}];
[_dataTask resume];
This code downloads an image(from a given url) and store it to memory+disk according to the http caching policy.
Deriving MyNetworkImageView from UIImageView and adding the above code to a setURL: method, is also straight forward.
My question is:
What are the advantages of using other third parties frameworks such as AFNetworking,FastImageCache,SDWebImage,SDImageCache?