UILabel层cornerRadius对性能产生负面影响

Nic*_*ick 45 calayer uiview uilabel ios

我创建了一个文档视图,显示了角落里的页码.页码是具有半透明背景颜色的uilabel,并且具有角半径(使用s 的cornerRadius属性).我把它放在了上面.但是,这会使滚动生涩.如果我删除,性能是好的.我能做些什么吗?什么是更好的解决方案?它似乎是在没有任何性能问题的情况下实现的.viewlayerUIScrollViewcornerRadiusUIWebView

小智 145

对于带有圆角和/或背景颜色的标签或视图以及滚动视图上的阴影,解决方案非常简单:

最大的问题来自masksToBounds图层选项.这似乎显着降低了性能,但是标签似乎需要此ON来将背景颜色掩盖为圆角.因此,要解决此问题,您需要设置标签图层背景颜色,并关闭masksToBounds.

第二个问题是默认行为是尽可能重绘视图,这对于滚动视图中的静态或慢速更改项是完全不必要的.这里我们只设置layer.shouldRasterize = YES.这将允许CA"缓存"视图的光栅化版本,以便在滚动时快速绘制(大概可以通过硬件加速).

您需要确保您的图层具有Alpha通道,否则栅格化将影响圆角的绘制.我从未遇到过问题,因为我为背景颜色设置了alpha,但您可能需要检查一下情况.

这是一个示例UILabel设置为在scollview上很好地工作:

UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 40.0, 24.0)];
lbl.font = [UIFont fontWithName:@"Helvetica" size:14.0];
lbl.textAlignment = UITextAlignmentRight;
lbl.text = @"Hello World";
// Must set the label background to clear so the layer background shows
lbl.backgroundColor = [UIColor clearColor];        
// Set UILabel.layer.backgroundColor not UILabel.backgroundColor otherwise the background is not masked to the rounded border.
lbl.layer.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5].CGColor;

lbl.layer.cornerRadius = 8;
lbl.layer.borderColor = [UIColor blackColor].CGColor;
lbl.layer.borderWidth = 1;
// Huge change in performance by explicitly setting the below (even though default is supposedly NO)
lbl.layer.masksToBounds = NO;
// Performance improvement here depends on the size of your view
lbl.layer.shouldRasterize = YES;
lbl.layer.rasterizationScale = [UIScreen mainScreen].scale;
// self here is the child view in the scroll view
[self addSubview:lbl];
[lbl release];
Run Code Online (Sandbox Code Playgroud)

我可以用这样的视图填充iPad 1屏幕并且仍然可以顺利滚动:)

  • @bioffe:可以通过设置光栅化比例轻松修复:`lbl.layer.rasterizationScale = [UIScreen mainScreen] .scale;` (17认同)
  • 顺便说一句,如果使用`.shouldRasterize = YES`,UILabel的内容在视网膜设备上看起来很脏. (4认同)
  • 这是完美的!但是UIImageView怎么样? (4认同)

Szu*_*_Jr 8

我也偶然发现了这个.cornerRadius(对于UIButton和UIImageView)正在杀死我的应用程序性能.我找不到使用背景图像对UIButton进行舍入的有效解决方案,因此我必须编写自己的代码 - 第一种方法.第二个添加圆角和边框到UIImageView.我认为代码说明了一切.

+(void)setBackgroundImage:(UIImage*)image forButton:(UIButton*)button withCornerRadius:(float)cornerRadius borderColor:(CGColorRef)borderColor andBorderWith:(float)borderWidth
{
    UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:button.frame];
    UIGraphicsBeginImageContextWithOptions(tempImageView.bounds.size, NO, 1.0);
    [[UIBezierPath bezierPathWithRoundedRect:tempImageView.bounds
                                cornerRadius:cornerRadius] addClip];
    [image drawInRect:tempImageView.bounds];
    tempImageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [button setBackgroundImage:tempImageView.image forState:UIControlStateNormal];
    button.layer.shouldRasterize = YES;
    button.layer.borderColor = borderColor;
    button.layer.borderWidth = borderWidth;
    button.layer.cornerRadius = cornerRadius;
}

+(void)makeRoundedCornersForImageView:(UIImageView*)imgV withCornerRadius:(float)cornerRadius borderColor:(CGColorRef)borderColor andBorderWith:(float)borderWidth
{
    UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:imgV.frame];
    UIGraphicsBeginImageContextWithOptions(tempImageView.bounds.size, NO, 1.0);
    [[UIBezierPath bezierPathWithRoundedRect:tempImageView.bounds
                                cornerRadius:cornerRadius] addClip];
    [imgV.image drawInRect:tempImageView.bounds];
    tempImageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    imgV.image = tempImageView.image;
    imgV.layer.shouldRasterize = YES;
    imgV.layer.borderColor = borderColor;
    imgV.layer.borderWidth = borderWidth;
    imgV.layer.cornerRadius = cornerRadius;
}
Run Code Online (Sandbox Code Playgroud)


Nic*_*ick 6

像petert建议的那样,在看到侧栏中的"相关"帖子后,我决定创建自己的图像.我将UIView子类化并添加了背景图像(对于带有圆边的背景)和init上的标准文本标签.为了创建背景图像,我使用CG绘图功能制作可伸缩的图像.

    // create background image
    CGFloat radius = frame.size.height / 2;
    CGFloat imgSize = (radius*2)+1; // 1 pixel for stretching
    UIGraphicsBeginImageContext(CGSizeMake(imgSize, imgSize));
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetAlpha(context, 0.5f);
    CGContextSetLineWidth(context, 0);
    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);

    CGFloat minx = 0;
    CGFloat midx = imgSize/2;
    CGFloat maxx = imgSize;
    CGFloat miny = 0;
    CGFloat midy = imgSize/2;
    CGFloat maxy = imgSize;

    CGContextMoveToPoint(context, minx, midy);
    CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathFillStroke);

    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImage *stretchImage = [viewImage stretchableImageWithLeftCapWidth:radius topCapHeight:radius];

    UIImageView *stretch = [[UIImageView alloc] initWithImage:stretchImage];
    stretch.frame = self.bounds;
    stretch.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
    [self addSubview:stretch];
    [self sendSubviewToBack:stretch];
    [stretch release];
Run Code Online (Sandbox Code Playgroud)