一.tableView的優(yōu)化
tableView作為iOS開發(fā)中使用最為頻繁的控件之一殃饿,對其進行優(yōu)化洗贰,對整個應用性能的提升顯得至關重要柏肪。官方設計的框架中召衔,已經(jīng)包含了UITableViewCell的重用機制大审。但是蘸际,tableView優(yōu)化除了cell的重用之外還有很多工作要做。
1徒扶,提前計算并緩存好行高粮彤。
cell行高不確定,需要根據(jù)內容來計算酷愧,tableView每次滾動時都會調用的兩個方法是:獲得cell的行高(cell大屑菡)和cell中的內容。這樣的話溶浴,就很有必要把行高緩存起來乍迄,具體做法就是在模型中添加行高的屬性。要不然士败,每 次滾動都要重新計算行高闯两。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dict = self.dataList[indexPath.row];
CGRect rect = [dict[@"frame"] CGRectValue];
return rect.frame.height;
}
2,異步繪制谅将,大幅提高性能漾狼。
cell上添加的控件,實質上系統(tǒng)都需要調用底層的接口進行繪制饥臂,當我們大量添加控件時逊躁,對資源的開銷也會很大,所以我們可以選擇直接繪制隅熙,調高效率稽煤。
//異步繪制
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
CGRect rect = [_data[@"frame"] CGRectValue];
UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
//整個內容的背景
[[UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:1] set];
CGContextFillRect(context, rect);
//轉發(fā)內容的背景
if ([_data valueForKey:@"subData"]) {
[[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1] set];
CGRect subFrame = [_data[@"subData"][@"frame"] CGRectValue];
CGContextFillRect(context, subFrame);
[[UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1] set];
CGContextFillRect(context, CGRectMake(0, subFrame.origin.y, rect.size.width, .5));
}
{
//名字
float leftX = SIZE_GAP_LEFT+SIZE_AVATAR+SIZE_GAP_BIG;
float x = leftX;
float y = (SIZE_AVATAR-(SIZE_FONT_NAME+SIZE_FONT_SUBTITLE+6))/2-2+SIZE_GAP_TOP+SIZE_GAP_SMALL-5;
[_data[@"name"] drawInContext:context withPosition:CGPointMake(x, y) andFont:FontWithSize(SIZE_FONT_NAME)
andTextColor:[UIColor colorWithRed:106/255.0 green:140/255.0 blue:181/255.0 alpha:1]
andHeight:rect.size.height];
//時間+設備
y += SIZE_FONT_NAME+5;
float fromX = leftX;
float size = [UIScreen screenWidth]-leftX;
NSString *from = [NSString stringWithFormat:@"%@ %@", _data[@"time"], _data[@"from"]];
[from drawInContext:context withPosition:CGPointMake(fromX, y) andFont:FontWithSize(SIZE_FONT_SUBTITLE)
andTextColor:[UIColor colorWithRed:178/255.0 green:178/255.0 blue:178/255.0 alpha:1]
andHeight:rect.size.height andWidth:size];
}
//將繪制的內容以圖片的形式返回核芽,并調主線程顯示
UIImage *temp = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
dispatch_async(dispatch_get_main_queue(), ^{
if (flag==drawColorFlag) {
postBGView.frame = rect;
postBGView.image = nil;
postBGView.image = temp;
}
}
//內容如果是圖文混排,就添加View酵熙,用CoreText繪制
[self drawText];
}}
這里是需要異步繪制轧简,但如果在重寫drawRect方法就不需要用GCD異步線程了,因為drawRect本來就是異步繪制的匾二。
3.使用不透明視圖哮独。
不透明的視圖可以極大地提高渲染的速度。因此如非必要察藐,可以將table cell及其子視圖的opaque屬性設為YES(默認值)皮璧。
其中的特例包括背景色,它的alpha值應該為1(例如不要使用clearColor)转培;圖像的alpha值也應該為1恶导,或者在畫圖時設為不透明浆竭。
當一個view是透明的浸须,iOS需要渲染一個像素兩次或多次,這是因為一個像素同時屬于很多subviews邦泄。這是一個非常耗時的過程删窒。
通過代碼或者InterfaceBuilder能夠很簡單的做到。開發(fā)者應該多次檢查所有的subviews是不透明的顺囊。
對于自定義代碼肌索,你可以通過代碼來設置,如下:view.opaque = YES;
4.不要重復創(chuàng)建不必要的table cell特碳。
前面說了诚亚,UITableView只需要一屏幕的UITableViewCell對象即可。因此在cell不可見時午乓,可以將其緩存起來站宗,而在需要時繼續(xù)使用它即可。
而UITableView也提供了這種機制益愈,只需要簡單地設置一個identifier即可:
static NSString *CellIdentifier = @"xxx";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
值得一提的是梢灭,cell被重用時,它內部繪制的內容并不會被自動清除蒸其,因此你可能需要調用setNeedsDisplayInRect:或setNeedsDisplay方法敏释。
5.對于重復使用的圖片使用imageNamed:來創(chuàng)建(這個會緩存),對于不需重復使用的摸袁,使用imageWithContentsOfFile钥顽。