Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用layer.shadow时的Objective-C性能优化_Objective C_Ios_Performance_Optimization - Fatal编程技术网

使用layer.shadow时的Objective-C性能优化

使用layer.shadow时的Objective-C性能优化,objective-c,ios,performance,optimization,Objective C,Ios,Performance,Optimization,我正在使用UITableView构建新闻提要列表。对于每一行,我使用QuartzCore层创建带阴影的矩形单元格。阴影 自定义UITableViewCellclass中的示例代码: -(void)drawRect:(CGRect)rect { UIView *bgView = [[UIView alloc] initWithFrame:self.bounds]; bgView.backgroundColor = [UIColor whiteColor]; bgView

我正在使用
UITableView
构建新闻提要列表。对于每一行,我使用QuartzCore层创建带阴影的矩形单元格。阴影

自定义
UITableViewCell
class中的示例代码:

-(void)drawRect:(CGRect)rect 
{
    UIView *bgView = [[UIView alloc] initWithFrame:self.bounds];
    bgView.backgroundColor = [UIColor whiteColor];

    bgView.layer.shadowColor = [UIColor blackColor].CGColor;
    bgView.layer.shadowOffset = CGSizeMake(0, 1);
    bgView.layer.shadowOpacity = 0.2;
    bgView.layer.shadowRadius = 1;

    self.backgroundView = bgView;
    [bgView release];
}
当我测试应用程序时,滚动
UITableView
,滚动性能很差!如果我去掉阴影,性能很好


我需要你的建议。为了获得最佳性能,我可以做什么样的优化?

您的问题不是Objective-C,而是阴影!由于iOS 3.2可以为阴影定义
CGPathRef
,因此应该构建一个仅包含视图轮廓的阴影,以减少渲染时间并提高性能。您还可以将阴影光栅化,以避免一直重新绘制阴影(将图层的
shouldRasterize
属性设置为
YES
。根据您希望对图层执行的操作,这可能不是最好的选项。记住,这也是一种内存/性能权衡!)


创建所需阴影路径的最简单方法应该是通过
UIBezierPath
类,该类有许多有用的类方法来构建各种形式的
CGPathRef
对象,但取决于视图的形状,您可能不得不回过头来手工构建自己的路径。

如何实现
UITableView
委托? 您是否使用重用创建了
UITableViewCell
,例如:


-[UITableView dequeueReusableCellWithIdentifier:]

请检查它。

我什么时候应该光栅化=是;阴影质量变差了。@AlmasAdilbek如我所说,这只是进一步提高性能的一个选项。您不必启用它,在某些情况下,它甚至会使结果更糟(并阻塞内存)。如果启用shouldRasterize,还必须设置rasterizationScale=[UIScreen mainScreen].scale,否则即使在视网膜设备上,它也会以标准def渲染(这可能就是它看起来不好的原因)。