UILabel层cornerRadius负面影响性能

我创build了一个文档视图,显示angular落中的页码。 页码是具有半透明背景颜色的uilabel,并具有angular半径(使用viewlayercornerRadius属性)。 我已经定位这上面的UIScrollView 。 但是,这使得滚动生涩。 如果我删除cornerRadius ,性能是好的。 有什么我可以做的呢? 什么是更好的解决scheme? 它似乎已经在UIWebView实现,没有任何性能问题。

对于标签,或滚动视图上的圆angular和/或背景颜色和阴影,解决scheme非常简单:

最大的问题是从masksToBounds图层选项。 这似乎显着的税收性能,但标签似乎需要这个ON掩盖背景颜色圆angular。 所以要解决这个问题,你需要设置标签图层的背景颜色,而不是关掉masksToBounds。

第二个问题是,默认行为是重新绘制视图,只要有可能,对于滚动视图,静态或缓慢更改项目是完全不必要的。 这里我们简单地设置layer.shouldRasterize = YES。 这将允许CA“滚动”视图的栅格化版本,以便在滚动时进行快速绘图(推测可能是通过硬件加速)。

您需要确保您的图层具有Alpha通道,否则栅格化会影响圆angular的绘制。 我从来没有遇到过问题,因为我为背景颜色设置了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]; 

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

我也偶然发现了这个。 cornerRadius(用于UIButton和UIImageView)正在杀死我的应用性能。 我发现没有任何工作的解决scheme,将UIButton与背景图像,所以我不得不写我自己的一段代码 – 第一个方法。 第二个添加一个圆angular和边界到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; } 

像petertbuild议的那样,在看到侧栏中的“相关”post后,我决定创build自己的图片。 我subclassed UIView并添加一个背景图像(对于圆angular边缘的背景)和一个标准的文本标签初始化。 要创build背景图像,我使用CG绘图function制作可伸缩图像。

  // 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]; 

我有一个与UILabel在一个自定义的UITableViewCell与圆angular类似的问题。 为了获得stream畅的表演,我“制作了”具有圆angular的图像来解决这个问题( 请参阅参考资料 )。

很多其他职位,包括这个 ,或者这可能会有所帮助。