我如何(成功)将maskView设置为UIView?

and*_*ius 5 mask objective-c uiview ios ios8

我正在制作一个基于相机的应用程序,并希望在相机预览上制作一个磨砂覆盖图,从磨砂视图中切出一个圆角矩形.

我一直在努力用UIView的maskLayer实现这一点,在iOS 8中引入并且已经非常不成功.

这是我的代码目前的样子:

// Blur the preview
UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIView *blurView = [[UIVisualEffectView alloc] initWithEffect:effect];
blurView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view insertSubview:blurView aboveSubview:imagePicker.view];

// Make blur view fill screen
{
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[blurView]-0-|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:NSDictionaryOfVariableBindings(blurView)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[blurView]-0-|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:NSDictionaryOfVariableBindings(blurView)]];
}

// Add a rounded of transparency to the blurView  **not working**
UIView *mask = [[UIView alloc] initWithFrame:CGRectMake(50.f, 50.f, 50.f, 50.f)];
mask.alpha = 1.0f;
mask.backgroundColor = [UIColor redColor];
[blurView setMaskView:mask]; // Remove this line, and everything is frosted.  Keeping this line and nothing is frosted. 
Run Code Online (Sandbox Code Playgroud)

正如我的评论所示,添加蒙版可以完全去除磨砂视图(更改蒙版的不透明度不起作用).我知道这不会产生我正在寻找的效果(它应该只在那个矩形中磨砂),但我不能让maskView以任何容量工作.

Chr*_*ris 6

UIView api说

一个可选视图,其alpha通道用于屏蔽视图的内容.

看到这个的最好方法是使用一个装有RGBA图像的UIImageView,它运行一个alpha通道 - 从photoshop打开透明导出的PNG就可以了.然后调整大小,将原点设置为0,0.

第二种方法是创建一个UIView,没有alpha ie,[UIColor clearColor]并添加一个具有alpha的子视图[UIColor whiteColor];

UIView *mask = [[UIView alloc] initWithFrame:(CGRect){0,0,100,100}];
mask.backgroundColor = [UIColor clearColor];
UIView *areaToReveal = [[UIView alloc] initWithFrame:(CGRect){20,20,50,50}];
areaToReveal.backgroundColor = [UIColor whiteColor];
[mask addSubview:areaToReveal];
_myView.maskView = mask;
Run Code Online (Sandbox Code Playgroud)

您可以使用添加x量半透明的其他UIViews [UIColor colorFromRed: green: blue: alpha:x];