IOS multi-threaded image loading and caching

Source: Internet
Author: User

Reprinted from http://blog.csdn.net/deep_explore/article/details/8144613

Use nsoperationqueue to load images in multiple threads and cache Images

Create a class CC

#import <Foundation/Foundation.h>@interface CC : NSOperation{    NSString *url;    NSString *imageName;    UIImage *image;    UIImageView *delegate;}@property (nonatomic,retain) NSString *url;@property (nonatomic,retain)NSString *imageName;@property (nonatomic,retain)UIImage *image;@property (nonatomic,retain)UIImageView *delegate;-(void)main;-(id) initWith:(NSString *)url imageName:(NSString *)imageName delegate:(UIImageView *)delegate;@end@implementation CC@synthesize url,delegate,image,imageName;-(id) initWith:(NSString *)url imageName:(NSString *)imageName delegate:(UIImageView *)delegate{    if (self = [super init]) {        self.url = [url retain];        self.imageName = imageName;        self.delegate = delegate;    }    return self;}-(void) main{    //    NSString *cachefile = [NSTemporaryDirectory() stringByAppendingPathComponent: self.imageName];    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.url]];    [data writeToFile:cachefile atomically:YES];        //    self.image = [UIImage imageWithData:data];    [self performSelectorOnMainThread:@selector(ok) withObject:nil waitUntilDone:NO];}-(void)ok{    [self.delegate setImage:self.image]}@end

Then, call

 

// The initialization queue of the member to the column = [[nsoperationqueue alloc] init]; // The image resources are in the array arry = [nsarray arraywithobjects: @ "http://static2.dmcdn.net/static/video/451/838/44838154:jpeg_preview_small.jpg? 20120509163826 ", @" http://static2.dmcdn.net/static/video/656/177/44771656:jpeg_preview_small.jpg? 20120509154705 ", @" http://static2.dmcdn.net/static/video/629/228/44822926:jpeg_preview_small.jpg? 20120509181018 ", @" http://static2.dmcdn.net/static/video/116/367/44763611:jpeg_preview_small.jpg? 20120509101749 ", @" http://static2.dmcdn.net/static/video/340/086/44680043:jpeg_preview_small.jpg? 20120509180118 ", @" http://static2.dmcdn.net/static/video/666/645/43546666:jpeg_preview_small.jpg? 20120412153140 ", @" http://static2.dmcdn.net/static/video/771/577/44775177:jpeg_preview_small.jpg? 20120509183230 ", @" http://static2.dmcdn.net/static/video/810/508/44805018:jpeg_preview_small.jpg? 20120508125339 ", @" http://static2.dmcdn.net/static/video/152/008/44800251:jpeg_preview_small.jpg? 20120508103336 ", @" http://static2.dmcdn.net/static/video/694/741/35147496:jpeg_preview_small.jpg? 20120508111445 ", @" http://static2.dmcdn.net/static/video/988/667/44766889:jpeg_preview_small.jpg? 20120508130425 ", @" http://static2.dmcdn.net/static/video/282/467/44764282:jpeg_preview_small.jpg? 20120507130637 ", @" http://static2.dmcdn.net/static/video/754/657/44756457:jpeg_preview_small.jpg? 20120507093012 ", @" http://static2.dmcdn.net/static/video/831/107/44701138:jpeg_preview_small.jpg? 20120506133917 ", @" http://static2.dmcdn.net/static/video/411/057/44750114:jpeg_preview_small.jpg? 20120507014914 ", @" http://static2.dmcdn.net/static/video/894/547/44745498:jpeg_preview_small.jpg? 20120509183004 ", @" http://static2.dmcdn.net/static/video/082/947/44749280:jpeg_preview_small.jpg? 20120507015022 ", @" http://static2.dmcdn.net/static/video/833/347/44743338:jpeg_preview_small.jpg? 20120509183004 ", @" http://static2.dmcdn.net/static/video/683/666/44666386:jpeg_preview_small.jpg? 20120505111208 ", nil]; implement image loading in tableview-(uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {nsstring * identifity = [nsstring stringwithformat: @ "identify % d", indexpath. section, indexpath. row]; uitableviewcell * cell = (uitableviewcell *) [tableview metadata: identifity]; If (cell = nil) {Cell = [[uitableviewcell alloc] initwithstyle: Invalid reuseidentifier: identifity]; cell. selectionstyle = uitableviewcellselectionstylenone; nsstring * filename = [nsstring stringwithformat: @ "% d", indexpath. row]; nslog (@ "FILENAME ===%@", filename); nsstring * cachefile = [nstemporarydirectory () stringbyappendingpathcomponent: Filename]; nslog (@ "cachefile =======----- % @", cachefile); uiimage * image = [uiimage imagewithcontentsoffile: cachefile]; If (image) {Cell. imageview. image = image; nslog (@ "cache available,") ;}else {nslog (@ "no cache,"); nslog (@ "arry ==% @", [arry objectatindex: indexpath. row]); CC * o = [[CC alloc] initwith: [[arry retain] objectatindex: indexpath. row] imagename: [nsstring stringwithformat: @ "% d", indexpath. row] delegate: cell. imageview]; [queue addoperation: O]; cell. imageview. image = [uiimage imagenamed: @ "default.png"]; // remember that arry requires a retain. Otherwise, the memory will crash when sliding back. [[arry retain] objectatindex: indexpath. row] This is correct }}

How to delete image Cache

# Define k_location_img_count 200 + (void) checklocationimgandremove {nsstring * apppath = [nshomedirectory () stringbyappendingstring: @ "/documents/images/permanentstore/"]; nsfilemanager * manager = [nsfilemanager defaultmanager]; If ([Manager fileexistsatpath: apppath]) {nsarray * locationimgarray = [Manager contentsofdirectoryatpath: apppath error: Nil]; if ([locationimgarray count]> [k_location_img_count Intvalue]) {[Manager removeitematpath: apppath error: Nil] ;}}@ "/documents/images/permanentstore/" custom, which is the path of the folder for caching images!

Clear cache in webview

Http://stackoverflow.com/questions/2523435/how-to-clear-uiwebview-cache

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.