IOS multi-image download and iOS image download
IOS multi-image download, image download in cell, and Cache Optimization.
(App. icon is the image address)
//First remove the picture from the memory cache
UIImage *image = self.images[app.icon];
If (image) {/ / picture in memory
cell.imageView.image = image;
}Else {/ / no pictures in memory
//Get library / caches folder
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
//Get file name
NSString *filename = [app.icon lastPathComponent];
//Calculate the full path of the file
NSString *file = [cachesPath stringByAppendingPathComponent:filename];
//Load file data for sandbox
NSData *data = [NSData dataWithContentsOfFile:file];
If (data) {/ / directly use the image in the sandbox
UIImage *image = [UIImage imageWithData:data];
cell.imageView.image = image;
//Save in dictionary
self.images[app.icon] = image;
}Else {/ / download pictures
[self.queue addOperationWithBlock:^{
//Download pictures, multithreading
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:app.icon]];
UIImage *image = [UIImage imageWithData:data];
[NSThread sleepForTimeInterval:1.0];
//Return to the main thread to display the picture
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
cell.imageView.image = image;
]
//Save in dictionary
self.images[app.icon] = image;
//Write picture file data to sandbox
[data writeToFile:file atomically:YES];
]
}