裁切图片(圆头像的实现)
1.还是那几个步骤
2.步骤详细在代码里
- (void)viewDidLoad {
[super viewDidLoad];
//加载图片
UIImage *image=[UIImage imageNamed:@"阿狸头像"];
//开启图片上下文
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
//裁切
//裁切范围
//以下两种方法都可以
UIBezierPath *path=[UIBezierPath bezierPathWithArcCenter:CGPointMake(image.size.width*0.5, image.size.height*0.5) radius:image.size.width*0.5 startAngle:0 endAngle:M_PI*2 clockwise:YES];
// UIBezierPath *path=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
[path addClip];
//[path fill];
//绘制图片
[image drawAtPoint:CGPointZero];
//从上下文中获得裁切好的图片
UIImage *image1=UIGraphicsGetImageFromCurrentImageContext();
//关闭图片上下文
UIGraphicsEndImageContext();
//显示
self.imageView.image=image1;
}
3.效果图片
- 1.
4.源代码的地址
- github
- http://git.oschina.net/HYLAppleDream/Quartz2D/blob/master/图片裁剪(圆头像图片)的实现/图片裁剪(圆头像图片)的实现/ViewController.m?dir=0&filepath=图片裁剪%28圆头像图片%29的实现%2F图片裁剪%28圆头像图片%29的实现%2FViewController.m&oid=32b651901d42093393ae67f9f3f42ed03f996c38&sha=37967f5f6bd0676c53142d1152ea6e03725e8259