iOS使视频背景模糊

标签 ios objective-c avplayer

我正在制作一个视频应用程序,我正在尝试添加视频的模糊背景 like that并根据幻灯片值控制模糊效果。我正在使用 Objectivc-C 进行编码。谁能帮帮我,我该怎么做?

最佳答案

你只需要一个 mask 模糊 View ,尝试使用这个代码:

@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

您可以将 mask 层的路径更改为您想要的任何形状,如果您需要对视频进行模糊处理,只需将 imageV 更改为您想要的 View 即可。

希望对您有所帮助。

关于iOS使视频背景模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39013324/

相关文章:

ios - 如何将显示在 UIWebView 中的 SVG 文件的元素居中?

iphone - Xcode 中的代码签名错误

ios - iOS Google Now 如何显示不同的卡片模板

ios - 将 AVPlayerLayer 添加为子层不会完全填充父 View

ios - Realm 迁移不起作用

ios - 在自动布局中,无论方向如何,如何让 View 占据屏幕的一半?

objective-c - 获取iOS中两个日期之间的日期名称?

ios - 在 arm64 上将字符串转换为整数?

ios - 当应用程序进入前台时,AVPlayer 状态会更新

ios - 如何使用 AVAssetExportSession 导出 AVPlayer 音频 mp3 文件?