通过绘制上下文的方式绘制图片圆角

//通过绘图的方式给图片设置圆角,而view的‘layer.cornerRadius’方法是离屏渲染,很消耗内存,所以可以通过下面的方法给图片设置圆角

//为imageView 添加类别,给imageView的图片设置圆角

-(void)imageWithCornerRadius:(CGFloat)radius

{

UIGraphicsBeginImageContextWithOptions(self.frame.size,NO, UIScreen.mainScreen.scale);

CGContextAddPath(UIGraphicsGetCurrentContext(), [UIBezierPathbezierPathWithRoundedRect:CGRectMake(0, 0,self.frame.size.width,self.frame.size.height)cornerRadius:radius].CGPath);

CGContextClip(UIGraphicsGetCurrentContext());

[self.imagedrawInRect:CGRectMake(0, 0,self.frame.size.width,self.frame.size.height)];

self.image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

}

//为UIImage添加生成圆角的API方法

-(UIImage*)imageWithCornerRadius:(CGFloat)radius

{

CGRect rect = (CGRect){0.f, 0.f,self.size};

UIGraphicsBeginImageContextWithOptions(self.size,NO, UIScreen.mainScreen.scale);

CGContextAddPath(UIGraphicsGetCurrentContext(), [UIBezierPathbezierPathWithRoundedRect:rectcornerRadius:radius].CGPath);

CGContextClip(UIGraphicsGetCurrentContext());

[selfdrawInRect:rect];

UIImage* image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

returnimage;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容