通过选择图像的一部分来创建具有边缘检测的蒙版

时间:2016-10-14 03:32:45

标签: ios swift gpuimage core-image

故事是,用户通过绘制线条或曲线来选择图像的一部分。根据选择,将使用边缘检测器创建掩模。

我怎样才能做到这一点?
CoreImage和GPUImage是否有助于实现这一目标? 边缘检测是否足以制作面具?

我对图像处理和操作没有任何高级知识。我只是一个尝试理解和学习的初学者。顺便说一句,这应该在iOS中完成。

就像this一样(掩蔽部分)。

1 个答案:

答案 0 :(得分:0)

我为你写了一个样本,向你展示如何使用蒙版来选择图像的一部分,如果你想要实现更复杂的效果,你只需要改变路径,然后就可以获得你想要的任何形状。

@interface MainViewController ()

@property UIImageView* imageV;
@property UIView* selectView;
@property UIView *blockView;
@property CGPoint startPoint;
@property CGPoint endPoint;

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UIBarButtonItem *recoverItem = [[UIBarButtonItem alloc] initWithTitle:@"Recover" style:UIBarButtonItemStyleDone target:self action:@selector(recover)];
    [self.navigationItem setLeftBarButtonItem:recoverItem];

    _imageV = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _imageV.image = [UIImage imageNamed:@"test"];
    [self.view addSubview:_imageV];

    _blockView = [[UIView alloc] init];
    _blockView.frame = _imageV.bounds;
    _blockView.backgroundColor = [UIColor whiteColor];
    [_imageV addSubview:_blockView];
    _blockView.hidden = true;

    _selectView = [[UIView alloc] init];
    _selectView.layer.borderColor = [UIColor greenColor].CGColor;
    _selectView.layer.borderWidth = 2;
    [_imageV addSubview:_selectView];
    _selectView.hidden = true;

    UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [self.view addGestureRecognizer:panGR];
}

- (void) handlePan: (UIPanGestureRecognizer *)pan{
    if (UIGestureRecognizerStateBegan == pan.state) {
        _startPoint = [pan locationInView:_imageV];
    }
    else if (UIGestureRecognizerStateChanged == pan.state) {
        _selectView.hidden = false;
        CGPoint currentP = [pan locationInView:_imageV];
        float rectWidth = currentP.x - _startPoint.x;
        float rectHeight = currentP.y - _startPoint.y;
        _selectView.frame = CGRectMake(_startPoint.x, _startPoint.y, rectWidth,rectHeight);
    }
    else if (UIGestureRecognizerStateEnded == pan.state) {
        _endPoint = [pan locationInView:_imageV];
        float rectWidth = _endPoint.x - _startPoint.x;
        float rectHeight = _endPoint.y - _startPoint.y;
        //create path
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRect:[UIScreen mainScreen].bounds];
        UIBezierPath *otherPath = [[UIBezierPath bezierPathWithRect:CGRectMake(_startPoint.x, _startPoint.y, rectWidth,rectHeight)] bezierPathByReversingPath];
        [maskPath appendPath:otherPath];
        //set mask
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.path = maskPath.CGPath;
        [_blockView.layer setMask:maskLayer];
        _blockView.hidden = false;

        _selectView.hidden = true;
    }
}

-(void) recover{
    _blockView.hidden = true;
}

@end

希望它可以帮到你。