UIButton 图层样式使应用程序变慢

Posted

技术标签:

【中文标题】UIButton 图层样式使应用程序变慢【英文标题】:UIButton layer styling makes application slow 【发布时间】:2014-06-21 17:43:15 【问题描述】:

我正在使用带有可滚动单元格的 UITableView。每个单元格包含近 20 到 25 个按钮,所有按钮都具有边框颜色和半径。因此,我的视图控制器演示非常慢。滚动表格视图时,应用程序也会变得太慢。

我尝试在创建按钮后复制它。但是复制按钮没有任何边框颜色/半径。这就是我赋予图层样式和重用按钮的方式。

button.layer.cornerRadius = 5;
button.layer.borderWidth = 1.0f;
button.layer.masksToBounds = YES;
button.backgroundColor = CRICHQ_GREY;
button.layer.borderColor = [UIColor lightGrayColor].CGColor;

为了复制,

NSData *dataSave = [NSKeyedArchiver archivedDataWithRootObject:oldButton];
UIButton *clonedButton = [NSKeyedUnarchiver unarchiveObjectWithData:dataSave];

但是当我复制按钮时,我没有在 clonedButton 按钮中获得图层样式。它给了我简单的按钮,所以我应该再次添加图层属性。

我搜索了很多链接,但没有得到任何好的解决方案。任何帮助将不胜感激。 提前致谢。

【问题讨论】:

这个帮助? ***.com/questions/6395139/… @DogCoffee 实际上不是。我不想让任何阴影只是边框。感谢您的帮助。 按钮图层上的.shouldRasterize 了吗 是的,我看到 iPhone 4 的性能很慢。我尝试了各种方法,但似乎没有什么能加快速度。我想你可能不得不重新考虑你的设计。为什么你还有这么多按钮?我没有尝试的一件事是圆形背景图像而不是使用图层。 我不明白您为什么认为重复使用按钮会提高性能。屏幕上的每个按钮都需要渲染,所以我看不出复制会有什么帮助。 【参考方案1】:

我不确定你反对使用背景图像而不是图层,但我试过了,它使设备上的滚动性能更好。表格视图控制器的初始加载仍然有点慢,但随后的加载速度很快。这是我用于测试的单元类中的代码,

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) 

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.layer.cornerRadius = 5;
        button.layer.borderWidth = 2.0f;
        button.layer.masksToBounds = YES;
        button.backgroundColor = [UIColor yellowColor];
        button.layer.borderColor = [UIColor darkGrayColor].CGColor;
        button.frame = CGRectMake(0,0, 50, 44);
        UIImage *bg = [self imageWithView:button];

        for (int i = 0; i<5; i++) 
            for (int j = 0; j<5; j++) 
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                NSString *title = [NSString stringWithFormat:@"BT-%d", j*5 + i];
                [button setTitle:title forState:UIControlStateNormal];
                [button addTarget:self action:@selector(doStuff:) forControlEvents:UIControlEventTouchUpInside];
                button.backgroundColor = [UIColor colorWithPatternImage:bg];
                button.frame = CGRectMake(60*i + 16, 50*j + 5, 50, 44);
                [self.contentView addSubview:button];
            
        
    
    return self;



- (UIImage *)imageWithView:(UIView *)view 
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(view.bounds.size.width, view.bounds.size.height), view.opaque, [[UIScreen mainScreen] scale]);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;

【讨论】:

你说得很好,所以我接受了你的回答,现在我使用的是图像而不是图层样式,这会产生良好的性能。感谢您的帮助。

以上是关于UIButton 图层样式使应用程序变慢的主要内容,如果未能解决你的问题,请参考以下文章

PySpark 添加执行器使应用程序变慢

谁知道photoshop中怎么使图片产生阴影效果

tvOS UIButton .plain 样式,没有模糊背景视图

单独线程中的屏幕捕获使 Java 应用程序变慢/无响应

启用 arch:SSE2 使程序变慢

html5 应用程序中的 gif 会使应用程序变慢?