iOS图片设置圆角

通常咱们在iOS开发的过程当中设置圆角都是以下这样设置的。ios

imageView.clipsToBounds = YES;

[imageView.layer setCornerRadius:50];

这样设置会触发离屏渲染,比较消耗性能。好比当一个页面上有十几头像这样设置了圆角缓存

会明显感受到卡顿。
这种就是最经常使用的,也是最耗性能的。
 
注意:ios9.0以后对UIImageView的圆角设置作了优化,UIImageView这样设置圆角
不会触发离屏渲染,ios9.0以前仍是会触发离屏渲染。而UIButton仍是都会触发离屏渲染。
 
第二种
imageView.clipsToBounds = YES;
imageView.layer setCornerRadius:50]; 
imageView.layer.shouldRasterize = YES;

shouldRasterize=YES设置光栅化,可使离屏渲染的结果缓存到内存中存为位图, 使用的时候直接使用缓存,节省了一直离屏渲染损耗的性能。性能

可是若是layer及sublayers经常改变的话,它就会一直不停的渲染及删除缓存从新 建立缓存,因此这种状况下建议不要使用光栅化,这样也是比较损耗性能的。优化

 

第三种spa

这种方式性能最好,可是UIButton上不知道怎么绘制,能够用UIimageView添加个 点击手势当作UIButton使用code

UIGraphicsBeginImageContextWithOptions(avatarImageView.bounds.size, NO, [UIScreen mainScreen].scale);
  [[UIBezierPath bezierPathWithRoundedRect:avatarImageView.bounds cornerRadius:50] addClip];
  [image drawInRect:avatarImageView.bounds];
  avatarImageView.image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

这段方法能够写在SDWebImage的completed回调里,也能够在UIImageView+WebCache.h 里添加一个方法,isClipRound判断是否切圆角,把上面绘制圆角的方法封装到里面。blog

相关文章
相关标签/搜索