gpt4 book ai didi

iOS - DrawRect 性能问题

转载 作者:行者123 更新时间:2023-11-28 19:18:07 27 4
gpt4 key购买 nike

我第一次使用 -drawRect 尝试加速 UITableView。但是,drawRect 方法似乎大大降低了表格的速度。

请您告诉我如何改进下面的drawRect 方法以加快表格速度?

编辑---

在 drawRect 方法中,我将两个 NSStrings 写入单元格的 View ,两个 UIImages 和一个投影到两个 NSStrings 和一个 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];
}

最佳答案

是的——您在您的应用程序上运行 Instruments 的 Time Profile 以准确告诉您花费了多少时间,以及在哪里。它会告诉您是图像、字符串、阴影还是其他内容。

关于iOS - DrawRect 性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11058489/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com