欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

iOS自定义裁剪区域,正方形圆形图片头像裁剪,仿QQ头像裁剪,圆形遮罩,矩型遮罩

程序员文章站 2022-03-24 20:13:21
...

最近项目中用到了自定义图片裁剪区域的图片裁剪功能,自己写了一个,可能有诸多不完善的地方,请大家指正。

支持任意区域裁剪,9:16裁剪、16:9裁剪、1:1裁剪、圆形裁剪等等,总之裁剪框的大小,裁剪框的区域都自定义;

如下截图:

iOS自定义裁剪区域,正方形圆形图片头像裁剪,仿QQ头像裁剪,圆形遮罩,矩型遮罩

整体思路:

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