UIImageView添加圆角的几种方法

1 离屏渲染但是比较消耗性能

imgView.layer.cornerRadius = 10;
imgView.clipsToBounds = YES; 

2 自定义一个接口来实现

- (UIImage *)hyb_imageWithCornerRadius:(CGFloat)radius {
  CGRect rect = (CGRect){0.f, 0.f, self.size};
  UIGraphicsBeginImageContextWithOptions(self.size, NO, UIScreen.mainScreen.scale);
   CGContextAddPath(UIGraphicsGetCurrentContext(), [UIBezierPath bezierPathWithRoundedRect:rect 
   cornerRadius:radius].CGPath);
   CGContextClip(UIGraphicsGetCurrentContext());
   [self drawInRect:rect];
   UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
 
posted @ 2017-04-07 21:31  小太阳521  阅读(324)  评论(0编辑  收藏  举报