Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用bezier路径自定义裁剪图像_Iphone_Ios_Image_Crop - Fatal编程技术网

Iphone 使用bezier路径自定义裁剪图像

Iphone 使用bezier路径自定义裁剪图像,iphone,ios,image,crop,Iphone,Ios,Image,Crop,Hi iam正在尝试制作一个自定义裁剪工具,使用户可以使用bezier路径创建自定义闭合区域 我用来剪辑带有路径的imageView的代码是 CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = clippingPath.CGPath; [imgView.layer setMask:maskLayer]; 但结果是 谁能给我指出正确的方向,

Hi iam正在尝试制作一个自定义裁剪工具,使用户可以使用bezier路径创建自定义闭合区域

我用来剪辑带有路径的imageView的代码是

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = clippingPath.CGPath;
[imgView.layer setMask:maskLayer];
但结果是

谁能给我指出正确的方向,我需要做什么

-(void)longPressAddPoint:(UITapGestureRecognizer*)gesture
{   ctr++;
    CGPoint  touchpoint=[gesture locationInView:self];
    [ptsNew addObject:[NSValue valueWithCGPoint:[gesture locationInView:self] ]];
    if(ctr>1)
    {
      if(CGRectContainsPoint([self viewWithTag:1].frame, [gesture locationInView:self]))
      {
          pathClosed=YES;
          touchpoint=[self viewWithTag:1].center;

      }

    UIView *greenCircle=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    greenCircle.center=touchpoint;
    greenCircle.backgroundColor=[UIColor greenColor];
        [greenCircle setTag:ctr];
    [greenCircle.layer setCornerRadius:10];
    [self addSubview:greenCircle];
        [pathMain appendPath:[pathCurrent bezierPathByReversingPath]];
        pathMain.usesEvenOddFillRule=YES;
        [pathCurrent moveToPoint:[[ptsNew objectAtIndex:ptsNew.count-2] CGPointValue]];
        [pathCurrent addCurveToPoint:[[ptsNew objectAtIndex:ptsNew.count-1] CGPointValue] controlPoint1:firstControllPoint.center controlPoint2:[[ptsNew objectAtIndex:ptsNew.count-1] CGPointValue]]; // this is how a Bezier curve is appended to a path
        [self setNeedsDisplay];


    }
    else {

        UIView *greenCircle=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
        greenCircle.center=touchpoint;
        greenCircle.backgroundColor=[UIColor greenColor];
        [greenCircle setTag:ctr];
        [greenCircle.layer setCornerRadius:10];
        [self addSubview:greenCircle];
    }
}
- (void)drawRect:(CGRect)rect
{   
    [pathCurrent stroke];
    [pathMain stroke];

}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *touch = [touches anyObject];
    if(CGRectContainsPoint(firstControllPoint.frame,[touch locationInView:self] )&&(ptsNew.count>=2))
    {
       flagForTouch=YES;
    }
    else {
        flagForTouch=NO;
    }

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
       if (flagForTouch) // 4th point
    {
        [pathCurrent removeAllPoints];
        [self setNeedsDisplay];
        [pathCurrent moveToPoint:[[ptsNew objectAtIndex:ptsNew.count-2] CGPointValue]];
        [pathCurrent addCurveToPoint:[[ptsNew objectAtIndex:ptsNew.count-1] CGPointValue] controlPoint1:firstControllPoint.center controlPoint2:[[ptsNew objectAtIndex:ptsNew.count-1] CGPointValue]]; // this is how a Bezier curve is appended to a path
        [self setNeedsDisplay];

    }
    if(flagForTouch)
    firstControllPoint.center=[[touches anyObject] locationInView:self];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesEnded:touches withEvent:event];
}

#pragma mark:- clipping image based on path
- (void) setClippingPath:(UIBezierPath *)clippingPath imageViewObj: (UIImageView *)imgView;
{
//    if (![[imgView layer] mask])
//        [[imgView layer] setMask:[CAShapeLayer layer]];
//    
//    [(CAShapeLayer*) [[imgView layer] mask] setPath:[clippingPath CGPath]];


    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    clippingPath.usesEvenOddFillRule=YES;
    maskLayer.path = clippingPath.CGPath;
    [imgView.layer setMask:maskLayer];
    UIGraphicsBeginImageContext(self.frame.size);
}

-(void)getCroppedImage:(sendImageBlock)Image
{
    sendImageToBase=Image;
}
-(void)cropIt
{
    if (pathClosed) {
        NSLog(@"path closed crop now");
        [pathMain appendPath:[pathCurrent bezierPathByReversingPath]];
        pathMain.usesEvenOddFillRule=YES;
        [self setClippingPath:[pathMain bezierPathByReversingPath] imageViewObj:self.imageToBeCropped];
        sendImageToBase(self.imageToBeCropped);
    }

}

我有一个主路径(作为剪切路径传递),我创建了用户可以向其中添加曲线的当前路径。当用户添加新路径时,当前路径将附加到主路径。

这里有一个很好的库来满足您的要求,如下所示:

要快速,请尝试这个:ZImageCropper

ZImageCropper使用以下内容作为核心部分:

尤比齐尔帕斯, 触摸事件, 现金支付者, 岩芯图


请检查并回复我。

谢谢,但我已经在做同样的事情了我的问题我没有得到正确的区域你能发布创建
clippingPath的代码吗
解决了问题我在添加曲线。我保存了点和控制点,并用它们来制作路径,现在它的工作状态很好,很久以前。。。我用任何方法制作钢笔工具画都不是完美的。