簡體   English   中英

iOS使視頻背景模糊

[英]iOS make video background blur

我正在制作視頻應用,並且嘗試添加視頻的模糊背景並根據幻燈片的值控制模糊效果。 我正在使用Objectivc-C進行編碼。 誰能幫我該怎么做?

您只需要蒙版模糊視圖,請嘗試使用以下代碼:

@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;
}

效果就像這張照片:

在此處輸入圖片說明

您可以將遮罩層的路徑更改為所需的任何形狀,如果需要模糊視頻,只需將imageV更改為所需的視圖即可。

希望它能對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM