故事是这样的,用户通过绘制直线或曲线来选择图像的一部分。根据选择,将使用边缘检测器创建掩模。
我怎样才能实现这个目标?
CoreImage 和 GPUImage 是否可以帮助实现这一目标?
边缘检测足以制作掩模吗?
我没有任何关于图像处理和操作的高级知识。我只是一个试图理解和学习它的初学者。顺便说一句,这应该在 iOS 中完成。
就像 this一个(掩蔽部分)。
最佳答案
我写了一个示例给你演示如何使用 mask 来选择图像的一部分,如果你想实现更复杂的效果,你只需要改变路径,就可以得到你想要的任何形状。
@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
希望对您有帮助。
关于ios - 通过选择图像的一部分来创建具有边缘检测的掩模,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40034511/