IOS:把用户上传头像裁剪为圆形
直接上代码:
UIImage*image =[UIImage imageNamed:@"testPic"];
//图片的宽度和高度
CGFloat imageWH = image.size.width;
//设置圆环的宽度
CGFloat border = 3;
//圆形的宽度和高度
CGFloat ovalWH = imageWH +2* border;
// 1.开启上下文
UIGraphicsBeginImageContextWithOptions(CGSizeMake(ovalWH, ovalWH),NO,0);
// 2.画大圆
UIBezierPath*path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0, ovalWH, ovalWH)];
//圆环颜色
[[UIColor redColor] set];
[path fill];
// 3.设置裁剪区域
UIBezierPath *clipPath =[UIBezierPath bezierPathWithOvalInRect:CGRectMake(border, border, imageWH, imageWH)];
[clipPath addClip];
// 4.绘制图片
[image drawAtPoint:CGPointMake(border, border)];
// 5.获取图片
UIImage *clipImage =UIGraphicsGetImageFromCurrentImageContext();
// 6.关闭上下文
UIGraphicsEndImageContext();
border:这是图片四周的圆环宽度
[[UIColor redColor] set];这是设置圆环颜色
clipImage 就是获取的裁剪好的图片