2012-06-15 32 views
0

我第一次使用-drawRect來嘗試加速UITableView。但是,drawRect方法似乎在很大程度上減緩了桌子的放置。iOS - DrawRect性能問題

請你能告訴我如何改進下面的drawRect方法以加快表格的速度嗎?

編輯---

在drawRect方法,我寫兩個NSString的到小區的觀點,二UIImages和陰影到兩個NSString的和UIImages之一。

其中一個上述圖像是異步下載的,然後調用setNeedsDisplay將UIImage繪製到屏幕上。我相信這最初可能是滯後發生的原因。

- (void) drawRect:(CGRect) rect { 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    [[UIColor clearColor] set]; 

    CGContextFillRect(context, rect); 

    CGContextSaveGState(context); 
    CGContextSetShadow(context, CGSizeMake(1,1),1); 
    //draw text here 

    if (shouldDrawImage == YES) { 

     CGContextDrawImage(context, CGRectMake(10, 10, 40, 40), self.image.CGImage); 

    } 

    CGContextDrawImage(context, CGRectMake(self.frame.size.width - 16, 0, 16, self.frame.size.height), [UIImage imageNamed:@"right_bar_including_holes"].CGImage); 

    NSString *authorName = [[self.info objectForKey:@"user"] objectForKey:@"full_name"]; 

    [RGB(219, 240, 73) set]; 

    CGSize maximumLabelSize = CGSizeMake(self.frame.size.width - 10 - 55 - 16,9999); 
    CGSize authorsize = [authorName sizeWithFont:[UIFont boldSystemFontOfSize:15] 
            constrainedToSize:maximumLabelSize 
             lineBreakMode:UILineBreakModeWordWrap]; 
    [authorName drawInRect:CGRectMake(60, 10, self.frame.size.width - 60, authorsize.height) withFont:[UIFont boldSystemFontOfSize:15]]; 

    [RGB(249,249,249) set]; 

    NSString *description = [self.info objectForKey:@"description"]; 
    CGSize descriptionSize = [description sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap]; 

    [description drawInRect:CGRectMake(60, authorsize.height + 15, descriptionSize.width, descriptionSize.height) withFont:[UIFont systemFontOfSize:14]]; 

    CGContextRestoreGState(context); 

} 

- (NSString *) reuseIdentifier { 
    return NSStringFromClass([SlideCell class]); 
} 

- (void) updateCellInfo:(NSDictionary *)_info { 
    [self setInfo:_info]; 

    UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)]; 
    [iv setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[[self.info objectForKey:@"user"] objectForKey:@"avatar"]]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *_image) { 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      shouldDrawImage = YES; 
      self.image = [self roundedImage:_image]; 
      [iv release]; 
      [self setNeedsDisplay]; 
     }); 

    } failure:nil]; 

    [self setNeedsDisplay]; 

    [self setSelectionStyle:UITableViewCellSelectionStyleNone]; 
} 
+1

這將幫助你的問題,如果你解釋爲什麼你正在使用的drawRect,你用它做什麼具體的,而不是指望有人通過您的代碼來查看可能存在的問題。 –

+0

@skinnyTOD我編輯了這個問題,謝謝你的輸入。 –

+1

覆蓋drawRect以提高性能似乎誤導了我。你正在假設你編寫了更高效的代碼,然後編寫了UITableView的人。我敢打賭,有一個更容易和更有效的解決方案。 – sosborn

回答

2

是 - 你對你的應用程序運行儀器的時間配置文件來告訴你到底有多少時間花在和其中。它會告訴你,如果它是圖像,字符串,陰影或其他東西。

1

您應該剖析該代碼,並首先查看圖像是否是問題。

我不知道如何在異步下載圖像時使用AFNetworking庫(您正在使用)。

如果您知道該圖像是問題,我懷疑圖像需要在UIImageView設置時重新縮放。這可能是問題所在。您需要將要設置爲UIImageView的UIImage重定標到UIIImageView的框架,因此沒有autorescaling觸發器。滾動時代價高昂。

您會收到圖像並立即向主線程發送代碼。潛在的重新標定可能在'引擎蓋下'工作。我想改變這種狀況的方法:

UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, IMGSIZEX, IMGSIZEY)]; 
    [iv setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[[self.info objectForKey:@"user"] objectForKey:@"avatar"]]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *_image) { 
     //Note you have to implement rescaleToFitSize 
     UIImage* rescaled = [_image rescaleToFitSize:iv.frame.size]; 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      self.image = _image; 
      [iv release]; 
      [self setNeedsDisplay]; 
     }); 

    } failure:^{ 
     //Handle failure! You create mem. leak on failure 
     [iv release]; 
}]; 

作爲一個側面說明,你不處理圖像下載失敗。你明確應該。

+0

同意。您可以從佔位符empy映像開始,並在異步下載完成後更新它(如果有問題的單元仍需要該映像)。 –

1

股票的UITableView是有效的,因爲它得到,只要你使用它正確:只要更多鈔票(alpha混合DOES慢下來),再利用細胞 3.配置沒有按 1.回收細胞(離隊) 2.避免透明度花費很多處理時間。

我不認爲任何人都可以通過重寫的drawRect蘋果的代碼顯著改善...

+1

如果您的單元格中有很多子視圖,滾動性能會受到影響。擁有一個自定義內容視圖並手動繪製全部視圖可以提高性能。 – jrturton

+1

是的,但使視圖不透明有很大幫助(例如,設置標籤背景顏色以匹配內容視圖的顏色,而不是clearColor)。無論如何,你應該重寫單元格的drawRect,而不是表格的。 –

+1

完全同意,我認爲提問者_is_覆蓋了一個單元格,而不是表格,但目前還不清楚。 – jrturton