不使用cornerRadius设置图片圆角
2016-09-08 22:33
441 查看
tableView的性能优化--不使用cornerRadius设置图片圆角
有人问我为什么tableView滑动不流畅,甚至闪退,其实和cell中的圆角头像使用了cornerRadius有关
优化点
行高一定要缓存不要动态创建子视图
所有子视图都要预先创建
所有的子视图都应该添加到 contentView上
普通
[imageView.layer setCornerRadius:50];// 设置圆角半径 imageView.clipsToBounds = YES;// 超出主层边框就要裁剪掉
第一种方法:通过设置layer的属性,实现圆角(这种方法在iOS9以前可能会造这样设置会触发离屏渲染,比较消耗性能。比如当一个页面上有十几头像这样设置了圆角
会明显感觉到卡顿。
这种就是最常用的,也是最耗性能的。
注意:ios9.0之后对UIImageView的圆角设置做了优化,UIImageView这样设置圆角
不会触发离屏渲染,ios9.0之前还是会触发离屏渲染。而UIButton还是都会触发离屏渲染。
方案一;shapeLayer的实现
#import <UIKit/UIKit.h> @interface UIImageView (Category) ///使用CAShapeLayer和UIBezierPath设置圆角 - (void) setRoundedCornersSize:(CGFloat)cornersSize ; @end
#import "UIImageView+Category.h" @implementation UIImageView (Category) - (void)setRoundedCornersSize:(CGFloat)cornersSize { UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:cornersSize]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath; self.layer.mask = maskLayer; } @end
其他方案
最好的是#pragma mark - 通过Graphics 和 BezierPath 设置圆角@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"keai"]]; imageView.backgroundColor = [UIColor yellowColor]; imageView.frame = CGRectMake(100, 100, self.view.frame.size.width - 200, self.view.frame.size.width - 200); [self.view addSubview:imageView]; // [self setGraphicsCutCirculayWithView:imageView roundedCornersSize:100]; [self setLayerAndBezierPathCutCircularWithView:imageView roundedCornersSize:100]; } #pragma mark - 通过设置layer 切圆角 - (void)setLayerCutCirculayWithView:(UIView *) view roundedCornersSize:(CGFloat )cornersSize { view.layer.masksToBounds = YES; // 设置圆角半径 view.layer.cornerRadius = cornersSize; } #pragma mark - 通过layer和bezierPath 设置圆角 - (void)setLayerAndBezierPathCutCircularWithView:(UIView *) view roundedCornersSize:(CGFloat )cornersSize { // 创建BezierPath 并设置角 和 半径 这里只设置了 左上 和 右上 UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(cornersSize, cornersSize)]; CAShapeLayer *layer = [[CAShapeLayer alloc] init]; layer.frame = view.bounds; layer.path = path.CGPath; view.layer.mask = layer; } #pragma mark - 通过Graphics 和 BezierPath 设置圆角 - (void)setGraphicsCutCirculayWithView:(UIImageView *) view roundedCornersSize:(CGFloat )cornersSize { UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 1.0); [[UIBezierPath bezierPathWithRoundedRect:view.bounds cornerRadius:cornersSize] addClip]; [view drawRect:view.bounds]; view.image = UIGraphicsGetImageFromCurrentImageContext(); // 结束 UIGraphicsEndImageContext(); } @end
相关文章推荐
- 【iOS】在不使用cornerRadius的情况下,设置图像为圆角
- 不使用layer的masksToBounds和cornerRadius来设置圆角
- 裁切图像设置圆角图像,不使用控件的layer.cornerRadius属性
- 使用CALayer的mask属性来设置图片圆角
- 如果需要将UIView的4个角全部都为圆角,做法相当简单,只需设置其Layer的cornerRadius属性即可
- iOS button设置背景图片后,设置cornerRadius没效果的问题
- Android 使用自定义Drawable 设置圆角矩形或者圆形图片
- Android L下用XXRadius设置圆角背景图片不能显示
- IOS如何给VIEW设置2个圆角?set cornerRadius for only top-left and top-right corner of a UIVIEW
- 还在用cornerRadius设置圆角么?快来试试效率更高的新方法。
- 使用ImageLoader设置圆角时无效或者干脆图片不显示
- iOS10 Xcode8 设置圆角 cornerRadius 后,控件不展示
- css利用border-radius圆角来设置背景图片为圆角的
- 设置UIView圆角 cornerRadius 圆角有性能问题,用贝塞尔曲线代替
- 给图片设置圆角 使用CALayer
- 使用Frasco将一个图片设置成圆角
- WPF/Silverlight TextBox 实现CornerRadius圆角
- 使用CSS设置网页图片透明度
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- Phone中图片的拉伸/圆角处理(Image round corner, aspect fit)