1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //圆环的宽度 CGFloat borderW=10; //加载旧的图片 UIImage *oldImage=[UIImage imageNamed:@"阿狸头像"]; //新的图片尺寸 CGFloat imageW=oldImage.size.width+2*borderW; CGFloat imageH=oldImage.size.height+2*borderW; //设置新的图片尺寸 CGFloat circleW=imageW>imageH?imageH:imageW; //开启上下文 UIGraphicsBeginImageContextWithOptions(CGSizeMake(circleW, circleW), NO, 0.0); //画大圆 UIBezierPath *path=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, circleW, circleW)]; //获取上下文 CGContextRef ctx=UIGraphicsGetCurrentContext(); CGContextAddPath(ctx, path.CGPath); //渲染 CGContextFillPath(ctx); CGRect clipR=CGRectMake(borderW, borderW, oldImage.size.width, oldImage.size.height); //画圆:正切于旧图片的圆 UIBezierPath *clipPath=[UIBezierPath bezierPathWithOvalInRect:clipR]; //设置裁剪区域 [clipPath addClip]; //画图片 [oldImage drawAtPoint:CGPointMake(borderW, borderW)]; //获取新的图片 UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext(); //关闭上下文 UIGraphicsEndImageContext();
_imageView.image=newImage; }
|