最近項目中用到了自定義圖片裁剪區域的圖片裁剪功能,自己寫了一個,可能有諸多不完善的地方,請大家指正。
支持任意區域裁剪,9:16裁剪、16:9裁剪、1:1裁剪、圓形裁剪等等,總之裁剪框的大小,裁剪框的區域都自定義;
如下截圖:
裁剪器.gif
整體思路:
1.利用scrollview的zoom縮放功能,縮放對象是所需裁剪圖片的imageView;
2.利用scrollView的contentInset參數,保證裁剪圖片在裁剪框內,并且可以裁剪圖片任意位置;
3.坐標轉換找出裁剪區域對應的在圖片中的位置,進行圖片的渲染裁剪。
使用方法
LZImageCropping *imageBrowser = [[LZImageCropping alloc]init];
//設置代理
imageBrowser.delegate = self;
//設置自定義裁剪區域大小
imageBrowser.cropSize = CGSizeMake(self.view.frame.size.width - 60, (self.view.frame.size.width-60));
//設置圖片
NSString *path = [[NSBundle mainBundle] pathForResource:@"IMG_1121" ofType:@"jpg"];
UIImage *image = [UIImage imageWithContentsOfFile:path];
cropper.image = image;
//是否需要圓形
imageBrowser.isRound = YES;
[self presentViewController:imageBrowser animated:YES completion:nil];
以下為關鍵代碼
二、自定義矩形遮罩 圓形遮罩 處理
透明區域是用了兩條貝塞爾曲線,第一條是整個灰色區域,然后第二條是需要透明的區域。將第二條附加的第一條上,并且設置填充規則為:
shapeLayer.fillRule = kCAFillRuleEvenOdd;
矩形遮罩
//矩形裁剪區域
- (void)transparentCutSquareArea{
//圓形透明區域
UIBezierPath *alphaPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, _selfWidth, _selfHeight)];
UIBezierPath *squarePath = [UIBezierPath bezierPathWithRect:_cropFrame];
[alphaPath appendPath:squarePath];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = alphaPath.CGPath;
shapeLayer.fillRule = kCAFillRuleEvenOdd;
self.overLayView.layer.mask = shapeLayer;
//裁剪框
UIBezierPath *cropPath = [UIBezierPath bezierPathWithRect:CGRectMake(_cropFrame.origin.x-1, _cropFrame.origin.y-1, _cropFrame.size.width+2, _cropFrame.size.height+2)];
CAShapeLayer *cropLayer = [CAShapeLayer layer];
cropLayer.path = cropPath.CGPath;
cropLayer.fillColor = [UIColor whiteColor].CGColor;
cropLayer.strokeColor = [UIColor whiteColor].CGColor;
[self.overLayView.layer addSublayer:cropLayer];
}
圓形裁剪遮罩
//圓形裁剪區域
-(void)transparentCutRoundArea{
CGFloat arcX = _cropFrame.origin.x + _cropFrame.size.width/2;
CGFloat arcY = _cropFrame.origin.y + _cropFrame.size.height/2;
CGFloat arcRadius;
if (_cropSize.height > _cropSize.width) {
arcRadius = _cropSize.width/2;
}else{
arcRadius = _cropSize.height/2;
}
//圓形透明區域
UIBezierPath *alphaPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, _selfWidth, _selfHeight)];
UIBezierPath *arcPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(arcX, arcY) radius:arcRadius startAngle:0 endAngle:2*M_PI clockwise:NO];
[alphaPath appendPath:arcPath];
CAShapeLayer *layer = [CAShapeLayer layer];
layer.path = alphaPath.CGPath;
layer.fillRule = kCAFillRuleEvenOdd;
self.overLayView.layer.mask = layer;
//裁剪框
UIBezierPath *cropPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(arcX, arcY) radius:arcRadius+1 startAngle:0 endAngle:2*M_PI clockwise:NO];
CAShapeLayer *cropLayer = [CAShapeLayer layer];
cropLayer.path = cropPath.CGPath;
cropLayer.strokeColor = [UIColor whiteColor].CGColor;
cropLayer.fillColor = [UIColor whiteColor].CGColor;
[self.overLayView.layer addSublayer:cropLayer];
}
三、裁剪圖片
裁剪矩形圖片
-(UIImage *)getSubImage{
//裁剪區域在原始圖片上的位置
CGRect myImageRect = CGRectMake(leftTopPoint.x * scaleRatio, leftTopPoint.y*scaleRatio, width, height);
//裁剪圖片
CGImageRef imageRef = self.image.CGImage;
CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);
UIGraphicsBeginImageContext(myImageRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, myImageRect, subImageRef);
UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
CGImageRelease(subImageRef);
UIGraphicsEndImageContext();
//是否需要圓形圖片
if (self.isRound) {
//將圖片裁剪成圓形
smallImage = [self clipCircularImage:smallImage];
}
return smallImage;
}
裁剪圓形圖片
//將圖片裁剪成圓形
-(UIImage *)clipCircularImage:(UIImage *)image{
CGFloat arcCenterX = image.size.width/ 2;
CGFloat arcCenterY = image.size.height / 2;
UIGraphicsBeginImageContext(image.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGContextAddArc(context, arcCenterX, arcCenterY, image.size.width/2, 0.0, 2*M_PI, NO);
CGContextClip(context);
CGRect myRect = CGRectMake(0 , 0, image.size.width , image.size.height);
[image drawInRect:myRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
最后
代碼很簡單,大家自己的需求可以直接在demo里面修改,具體代碼詳見github,幫助到你的話,可以隨手給個星星哦!
代碼: https://github.com/FirstStepzz/LZImageCropping.git