介绍图片剪裁,以剪裁圆形为例
普通剪裁
![这里写图片描述](https://img-blog.csdn.net/20170803103413835?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ2V6aTA2MzA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
带圆环的图片
![这里写图片描述](https://img-blog.csdn.net/20170803103505977?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ2V6aTA2MzA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
拖入一个UIImageView,取名“imageView”到view中
UIImage * image = [UIImage imageNamed:@"阿狸"];
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
[path addClip];
[image drawAtPoint:CGPointZero];
UIImage * imageNew = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.imageView.image = imageNew;
- 裁剪出带圆环的图片(给UIImage添加一个类目方法)
/**
裁剪一个带圆环的图片
@param image 图片
@param borderWidth 圆环宽度
@param color 圆环颜色
@return 裁剪完成的图片
*/
+(UIImage*)imageWithClipImage:(UIImage*)image borderWidth:(CGFloat)borderWidth borderColor:(UIColor*)color
{
CGFloat imageWH = image.size.width;
CGFloat border = borderWidth;
CGFloat ovalWH = imageWH + 2 * border;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(ovalWH, ovalWH), NO, 0);
UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, ovalWH, ovalWH)];
[color set];
[path fill];
UIBezierPath * clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(border, border, imageWH, imageWH)];
[clipPath addClip];
[image drawAtPoint:CGPointMake(border, border)];
UIImage * clipImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return clipImage;
}