IOS multi-image download and iOS image download

Source: Internet
Author: User

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];
]
} 

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.