这里提供了两种裁剪圆形图片的方法
1.通过对设置imageView中layer的圆角半径实现圆形图片
// 设置圆角半径imageView.layer.cornerRadius = imageView.width * 0.5;
// 超出主层的部分裁减掉
imageView.layer.masksToBounds = YES;
2.通过上下文对图片进行裁剪实现圆形图片
// 1.开启图形上下文
UIGraphicsBeginImageContext(image.size);
// 2.描述圆形裁剪区域
UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
// 3.设置裁剪区域
[clipPath addClip];
// 4.绘图
[image drawAtPoint:CGPointZero];
// 5.取出图片
image = UIGraphicsGetImageFromCurrentImageContext();
// 6.关闭上下文
UIGraphicsEndImageContext();
imageView.image = image;
本文介绍了两种将图片裁剪为圆形的方法:一种是通过设置UIImageView的layer属性来实现;另一种是利用图形上下文和UIBezierPath进行精确裁剪。


被折叠的 条评论
为什么被折叠?



