我第一次使用-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