iOS Edge Learning-multi-image downloads for multi-threaded exercises and multi-image downloads using a third-party framework (Sdwebimage)

Source: Internet
Author: User

First, self-realization of multi-image download should pay attention to the problem

    • Problems with sandbox caching
    • Problems with program caching
    • Cell reuse Display Picture confusion problem-user drag fast, download pictures slow caused by
    • Solve the problem of picture confusion introducing Nsoperation collection
    • The issue of resource download failure (not seen in practice, but must be considered)
1 #import "ChaosViewController.h"2 #import "ChaosApp.h"3 4 @interfaceChaosviewcontroller ()5 /** Model Collection*/6@property (nonatomic,strong) Nsmutablearray *apps;7 /** Picture Cache*/8@property (nonatomic,strong) nsmutabledictionary *Imagecache;9 Ten /** Queue*/ One@property (nonatomic,strong) Nsoperationqueue *queue; A  - /** All objects of Operation*/ -@property (nonatomic,strong) nsmutabledictionary *opeartions; the  - @end -  - @implementationChaosviewcontroller +  --(Nsmutabledictionary *) Opeartions + { A     if(_opeartions = =Nil) { at          -_opeartions =[Nsmutabledictionary dictionary]; -          -     } -     return_opeartions; - } in  --(Nsoperationqueue *) Queue to { +     if(_queue = =Nil) { -          the         //set maximum number of threads *_queue.maxconcurrentoperationcount =3; $         Panax Notoginseng_queue =[[Nsoperationqueue alloc] init]; -     } the     return_queue; + } A  the-(Nsmutabledictionary *) Imagecache + { -     if(_imagecache = =Nil) { $          $_imagecache =[Nsmutabledictionary dictionary]; -     } -     return_imagecache; the } - Wuyi-(Nsmutablearray *) Apps the { -     if(_apps = =Nil) { Wu          -_apps =[Nsmutablearray array]; About          $NSString *path = [[NSBundle mainbundle] Pathforresource:@"apps.plist"Oftype:nil]; -Nsarray *arrdict =[Nsarray Arraywithcontentsoffile:path]; -          -          for(Nsdictionary *dictincharrdict) { AChaosapp *app =[Chaosapp appwithdict:dict]; + [_apps Addobject:app]; the         } -     } $      the     return_apps; the } the  the- (void) Viewdidload { - [Super Viewdidload]; in } the  the #pragmaMark-table View Data source About  the-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section { the      the     returnSelf.apps.count; + } -  the Bayi-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath { the      theUITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:@"app"]; -      -Chaosapp *app =Self.apps[indexpath.row]; the      theCell.textLabel.text =App.name; theCell.detailTextLabel.text =App.download; the      -UIImage *image =Self.imagecache[app.icon]; the      the     if(image) {//There are pictures in the cache the         94Cell.imageView.image =image; the          the}Else{//not in the cache, look for a picture in the system sandbox the         98         //Get Library\cache file AboutNSString *cachepath =[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject]; -         //gets the name of the picture to get101NSString *filename =[App.icon lastpathcomponent];102         //stitching picture file path103NSString *fullpath =[CachePath stringbyappendingpathcomponent:filename];104          the         //get NSData through the picture full path106NSData *data = nil;//[NSData Datawithcontentsoffile:fullpath];107         108         if(data) {//There are pictures in the sandbox109              theCell.imageView.image =[UIImage Imagewithdata:data];111              the}Else{//the sandbox cache file also has no113             //set the position picture-function: The system's ImageView default does not have the size, if the first picture has not shown, the user drags and then returns, the picture download completes also does not display. In fact, ImageView already have pictures, but imageview no size can not see.  theCell.imageView.image = [UIImage imagenamed:@"placeholder"]; the              theNsoperation *operation = Self.opeartions[app.icon];//operation of the corresponding picture from the action set117             if(Operation = =Nil) {118Operation = [Nsblockoperation blockoperationwithblock:^{119                      -                     //Download Image121NSData *data =[NSData Datawithcontentsofurl:[nsurl URLWithString:app.icon];122                     123                     if(Data = =Nil) {124 [Self.opeartions RemoveObjectForKey:app.icon]; the                         return;126                     }127                      -UIImage *image =[UIImage Imagewithdata:data];129 //[Nsthread sleepfortimeinterval:1.0];//after the thread sleeps for a second, the cell picture appears to be confusing the                     //The downloaded picture is stored in the cache collection, App.icon as the key image as the value131Self.imagecache[app.icon] =image; the                     133[[Nsoperationqueue Mainqueue] addoperationwithblock:^{134                         //back to main thread display picture135 [TableView Reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationnone]; 136                     }];137                     138                     //to write a picture to the sandbox cache file139 [Data Writetofile:fullpath atomically:yes]; $                     141 [Self.opeartions RemoveObjectForKey:app.icon];142                 }];143             }144             145 [Self.queue addoperation:operation];146Self.opeartions[app.icon] =operation;147         }148     }149     returncell; Max }151  the 153 @end

Second, after the use of the Sdwebimage framework, all the above worries are not considered.

iOS Edge Learning-multi-image downloads for multi-threaded exercises and multi-image downloads using a third-party framework (Sdwebimage)

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.