iOS使视频背景模糊

时间:2016-08-18 08:27:34

标签: ios objective-c avplayer

我正在制作视频应用,我正在尝试添加视频like that的模糊背景,并根据幻灯片值控制模糊效果。我正在使用Objectivc-C进行编码。任何人都可以帮助我,我该怎么做?

1 个答案:

答案 0 :(得分:1)

您只需要一个遮罩模糊视图,尝试使用此代码:

@interface ViewController ()

@property UISlider *slider;
@property UIVisualEffectView *visualEffectView;

@end

@implementation ViewController

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

    //VisableRect = 100,100,200,200

    //Add a backgroun picture
    UIImageView* imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 100)];
    imageV.image = [UIImage imageNamed:@"test"];
    [self.view addSubview:imageV];

    _visualEffectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    _visualEffectView.frame = imageV.frame;
    _visualEffectView.alpha = 1.0;
    [self.view addSubview:_visualEffectView];

    //create path
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRect:imageV.bounds];
    UIBezierPath *otherPath = [[UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 200, 200)] bezierPathByReversingPath];
    [maskPath appendPath:otherPath];

    //set mask
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = maskPath.CGPath;
    [_visualEffectView.layer setMask:maskLayer];

    _slider = [[UISlider alloc] initWithFrame:CGRectMake(0, imageV.frame.size.height, 200, 20)];
    _slider.minimumValue = 0;
    _slider.maximumValue = 1;
    _slider.value = 1;
    [_slider addTarget:self action:@selector(updateValue:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_slider];
}

-(IBAction)updateValue:(id)sender{
    float sVaLue = _slider.value;
    _visualEffectView.alpha = sVaLue;
}

效果如下图所示:

enter image description here

您可以将遮罩层的路径更改为您想要的任何形状,如果需要模糊视频,只需将imageV更改为您想要的视图。

希望它可以帮到你。