image 圆角

来源:互联网 发布:淘宝药店的药是真的吗 编辑:程序博客网 时间:2024/05/28 15:20


方法一:效率高
 


 aImageView.layer.cornerRadius = aImageView.frame.size.width/2.0;  
   aImageView.layer.masksToBounds = YES;


方法二:效率低


CAShapeLayer *layer = [CAShapeLayer layer];  
UIBezierPath *aPath = [UIBezierPath bezierPathWithOvalInRect:aImageView.bounds];  
layer.path = aPath.CGPath;  
aImageView.layer.mask = layer;


此时,有layer 的离屏渲染,这会进行上下文的切换,导致效率下降。


解决的方法:
 1,不要在滚动视图使用cornerRadius或者mask。
 2,如果你非要做,那么这样也可以拯救你: 
   self.layer.shouldRasterize = YES;  
   self.layer.rasterizationScale = [UIScreen mainScreen].scale;
     shouldRasterize = YES会使视图渲染内容被缓存起来,下次绘制的时候可以直接显示缓存,当然要在视图内容不改变的情况下。
3,还是采取预先生成圆角图片,并缓存起来这个方法才是比较好的手段。预处理圆角图片可以在后台处理,处理完毕后缓存起来,再在主线程显示,这就避免了不必要的离屏渲染了。

4,在图片上面覆盖一个镂空圆形图片的方法可以实现圆形头像效果,这个也是极为高效的方法。缺点就是对视图的背景有要求,单色背景效果就最为理想。


方法三:


 UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
 [[UIBezierPath bezierPathWithRoundedRect:RECT     cornerRadius:RADIUS] addClip];
 [image drawInRect:RECT];
 UIImage* imageNew =     UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();


参考:http://www.cocoachina.com/ios/20150803/12873.html

0 0