我第一次使用-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];
}
這將幫助你的問題,如果你解釋爲什麼你正在使用的drawRect,你用它做什麼具體的,而不是指望有人通過您的代碼來查看可能存在的問題。 –
@skinnyTOD我編輯了這個問題,謝謝你的輸入。 –
覆蓋drawRect以提高性能似乎誤導了我。你正在假設你編寫了更高效的代碼,然後編寫了UITableView的人。我敢打賭,有一個更容易和更有效的解決方案。 – sosborn