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

2016-11-17  本文已影响0人  京哥

//通过绘图的方式给图片设置圆角,而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;

}

上一篇下一篇

猜你喜欢

热点阅读