A download operation is given to a Hmdownloadoperation object
HMDownloadOperation.h/. M
@class hmdownloadoperation; @protocol Hmdownloadoperationdelegate <nsobject> @optional -( void ) Downloadoperation: (hmdownloadoperation *) operation Didfinishdownload: (UIImage * @end @interface Hmdownloadoperation:nsoperation@property (nonatomic, copy) NSString *url; @property (nonatomic, strong) Nsindexpath *indexpath ; @property (nonatomic, weak) id <HMDownloadOperationDelegate> Span style= "COLOR: #0000ff" >delegate ; @end
View Code
#import "HMDownloadOperation.h"@implementationhmdownloadoperation/** * Implement specific actions in the Main method*/- (void) main{@autoreleasepool {nsurl*downloadurl =[Nsurl URLWithString:self.url]; NSData*data = [NSData Datawithcontentsofurl:downloadurl];//This guild is time consuming .UIImage *image =[UIImage Imagewithdata:data]; if([Self.Delegaterespondstoselector: @selector (downloadoperation:didfinishdownload:)]) {Dispatch_async (Dispatch_get_main_queue (),^{//back to the main thread, pass the picture data to the proxy object[Self.Delegatedownloadoperation:self Didfinishdownload:image]; }); } }}View Code
Controller View Call
@interfaceHmviewcontroller () <HMDownloadOperationDelegate>@property (nonatomic, strong) Nsarray*apps; @property (Nonatomic, strong) Nsoperationqueue*queue;/** Key:url Value:operation Object*/@property (nonatomic, strong) Nsmutabledictionary*operations;/** Key:url Value:image Object*/@property (nonatomic, strong) Nsmutabledictionary*images;@end@implementationHmviewcontroller-(Nsarray *) apps{if(!_apps) {Nsarray*dictarray = [Nsarray arraywithcontentsoffile:[[nsbundle mainbundle] Pathforresource:@"apps.plist"Oftype:nil]]; Nsmutablearray*apparray =[Nsmutablearray array]; for(Nsdictionary *dictinchDictarray) {Hmapp*app =[Hmapp appwithdict:dict]; [Apparray Addobject:app]; } _apps=Apparray; } return_apps;}-(Nsoperationqueue *) queue{if(!_queue) {_queue=[[Nsoperationqueue alloc] init]; _queue.maxconcurrentoperationcount=3;//Maximum number of concurrent = = 3 } return_queue;}-(Nsmutabledictionary *) operations{if(!_operations) {_operations=[Nsmutabledictionary dictionary]; } return_operations;}-(Nsmutabledictionary *) images{if(!_images) {_images=[Nsmutabledictionary dictionary]; } return_images;}- (void) viewdidload{[Super Viewdidload]; }#pragmaMark-Data Source Method-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{returnSelf.apps.count;}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *id =@"app"; UITableViewCell*cell =[TableView Dequeuereusablecellwithidentifier:id]; if(!cell) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:id]; } Hmapp*app =Self.apps[indexpath.row]; Cell.textLabel.text=App.name; Cell.detailTextLabel.text=App.download; //Show Pictures//ensure that a URL corresponds to a hmdownloadoperation//ensure that a URL corresponds to a UIImage objectUIImage*image =Self.images[app.icon]; if(image) {//There are pictures in the cacheCell.imageView.image =image; } Else{//There are no pictures in the cache, you have to downloadCell.imageView.image = [UIImage imagenamed:@"57437179_42489b0"]; Hmdownloadoperati on*operation = Self.operations[app.icon]; if(operation) {//is downloading// ... There's no need to do anything else at the moment } Else{//not currently downloading//Create actionOperation =[[Hmdownloadoperation alloc] init]; Operation.url=App.icon; Operation.Delegate=Self ; Operation.indexpath=Indexpath; [Self.queue addoperation:operation]; //Asynchronous DownloadSelf.operations[app.icon]=operation; } } //Sdwebimage: Dedicated to downloading pictures returncell;}#pragmamark-hmdownloadoperationdelegate-(void) Downloadoperation: (hmdownloadoperation *) operation Didfinishdownload: (UIImage *) image{//1. Remove the completed Operation[Self.operations RemoveObjectForKey:operation.url]; if(image) {//2. Put the picture in the cache (images)Self.images[operation.url] =image; //3. Refresh the table[Self.tableview Reloadrowsatindexpaths:@[operation.indexpath] Withrowanimation:uitableviewrowanimationnone] ; //3. Write the picture to the sandbox//NSData *data = uiimagepngrepresentation (image);//[Data writetofile:@ "" atomically:<# (BOOL) #>]; } }- (void) Scrollviewwillbegindragging: (Uiscrollview *) scrollview{//Start dragging//Pause Queue[Self.queue Setsuspended:yes];}- (void) Scrollviewwillenddragging: (Uiscrollview *) ScrollView withvelocity: (cgpoint) Velocity targetcontentoffset: (inout Cgpoint *) targetcontentoffset{[Self.queue setsuspended:no];}@end
IOS custom operation (download feature)