您的位置:首页 > 移动开发 > IOS开发

iOS设置图片圆角--防止离屏渲染

2017-05-19 18:42 302 查看

1. 扩展UIImage,添加类方法,返回圆角图片:

- (UIImage *)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;
}

2.通过mask遮罩

...

imgView.image = [UIImage imageNamed:@"test"];
//设置图片上面两个圆角
r = 2.f;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imgView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(r, r)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = imgView.bounds;
maskLayer.path = maskPath.CGPath;
pictureView_.layer.mask = maskLayer;
...




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: