runloop UITableViewCell Loading high-definition large image speed optimization2017-02-14 14:25 Source: Clear Screen Network popularity: 73 Comments (0)
iOS development, UITableView optimization has always been a commonplace problem, in addition to the usual pre-loading, caching and other methods, in fact, the use of Runloop cycle can also achieve the smooth loading of large map, specific use of the method we use a demo to understand in detail:
First of all, we have a need to load high-definition images from the network to UITableViewCell, and each cell loaded with more than one image, when the number of cells too many, we need to maintain smoothness and load speed.
So let's do a simple analysis:
1, because this is used in the Runloop cycle, then we can hear the runloop each cycle,
in each cycle we consider to do a picture download and layout.
2, since to perform a task in each loop,
we can first add all the load of the task code block to an array,
each time we loop out the first task to execute. *
3, because runloop in idle time will automatically hibernate,
so we have to find a way to let Runloop always in the loop state.
Okay, so we can start thinking about code implementations: The first step is to implement the UITableView basic effect First
@property (strong,nonatomic) uitableview* Showimagetableview; Lazy Loading-(UITableView *) showimagetableview{if (!_showimagetableview) {_showimagetableview = [[UITableView all]
Oc]initwithframe:[uiscreen Mainscreen].bounds Style:uitableviewstyleplain];
_showimagetableview.backgroundcolor = [Uicolor Yellowcolor];
_showimagetableview.delegate = self;
_showimagetableview.datasource = self;
} return _showimagetableview; }//Register [Self.showimagetableview Registerclass:[uitableviewcell class] Forcellreuseidentifier:
Showimagetableviewreusableidentifier];
Add [Self.view AddSubview:self.showImageTableView]; Data source proxy #pragma mark-uitableviewdelegate-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{uitableviewcell* cell = [TableView dequeuereusablecellwithidentifier
: Showimagetableviewreusableidentifier];
Add 3 images per cell for (int i = 0; i < 3; i++) {#这里是添加图片的方法} return cell;
}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return 399;} -(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{return 135;}
The second step is to initialize the variable group to store the task (block of code)
typedef void (^savefuncblock) ();
Array of storage tasks
@property (nonatomic, strong) Nsmutablearray *savetaskmarr;
The maximum number of tasks (tasks that exceed the maximum number of tasks will stop executing)
@property (nonatomic, assign) Nsinteger Maxtasksnumber;
The code block @property the task execution
(nonatomic, copy) Savefuncblock Savefuncblock;
-(Nsmutablearray *) savetaskmarr{
if (!_savetaskmarr) {
_savetaskmarr = [Nsmutablearray array];
}
return _savetaskmarr;
}
Self.maxtasksnumber = 18;
The third step is to create a new cell method to add a picture
-(void) Addimagetocell: (uitableviewcell*) cell Andtag: (nsinteger) tag{uiimageview* cellimageview = [[UIImageView allo
C]initwithframe:cgrectmake (tag* (imagewidth+5), 5, ImageWidth, ImageHeight)]; Dispatch_async (Dispatch_get_global_queue (0,0), ^{