我正在制作一个视频应用程序,我正在尝试添加视频的模糊背景 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;
}
效果是这样的:
您可以将 mask 层的路径更改为您想要的任何形状,如果您需要对视频进行模糊处理,只需将 imageV 更改为您想要的 View 即可。
希望对您有所帮助。
关于iOS使视频背景模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39013324/