如何从蒙版图像中移动和裁剪图像?

时间:2015-08-11 16:33:27

标签: ios objective-c image crop masking

我在以下链接的帮助下成功屏蔽了图像:

How to Mask an UIImageView

在上面的链接中有两个名为 image.png mask.png 的图片,

屏蔽图像后,我想裁剪结果图像。

我担心的是我要裁剪名为 image.png 的图像,但 mask.png 应该保持原样。我正在使用KICropImageView https://github.com/zhangzhixun/CropImageDemo来裁剪图片。

但是当我滚动图片时,整个结果图片正在滚动,但我只想滚动 image.png 而不是 mask.png 图片

知道我该怎么做?

1 个答案:

答案 0 :(得分:0)

你可以使用PanGesture ..

- (void)viewDidLoad
{ 

[super viewDidLoad];

UIPanGestureRecognizer *pan1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanImage:)];

self.imageview.userInteractionEnabled = YES;

[self.imageview addGestureRecognizer:pan1];

}

**after called Method handlePanImage..**

- (void)handlePanImage:(UIPanGestureRecognizer *)sender

{

 static CGPoint originalCenter;

 if (sender.state == UIGestureRecognizerStateBegan)


 {

 originalCenter = sender.view.center;

        sender.view.alpha = 0.8;

        [sender.view.superview bringSubviewToFront:sender.view];

    }

    else if (sender.state == UIGestureRecognizerStateChanged)

    {
        CGPoint translation = [sender translationInView:self.view];

        sender.view.center = CGPointMake(originalCenter.x + translation.x, originalCenter.y + translation.y);

    }

    else if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateCancelled || sender.state == UIGestureRecognizerStateFailed)
    {
        // do whatever post dragging you want, e.g.
        // snap the piece into place

        [UIView animateWithDuration:0.2 animations:^{
            CGPoint center = sender.view.center;
            center.x = round(center.x / 50.0) * 50.0;
            center.y = round(center.y / 50.0) * 50.0;
            sender.view.center = center;
            sender.view.alpha  = 1.0;
        }];
    }
}