gpt4 book ai didi

ios - 如何使用 UISlider 更改视频的 CIFilter 值?

转载 作者:行者123 更新时间:2023-11-29 05:29:14 25 4
gpt4 key购买 nike

我正在尝试将 CIFilter 添加到视频中。但我使用 UISliders 更改值,因为 AVPlayer 需要时间来播放添加过滤器的视频。我搜索了这个问题,UISlider 值不断变化,并且它也需要浮点值。因为每次过滤器都会添加到视频并播放。我想知道当 slider 值连续变化时如何更改 CIFilter 的值并且不延迟 AVPlayer。

float value = sender.value;
CIFilter *filter = [CIFilter filterWithName:@"CIColorControls"];
AVVideoComposition *composition = [AVVideoComposition videoCompositionWithAsset:tempAVAsset applyingCIFiltersWithHandler:
^(AVAsynchronousCIImageFilteringRequest *request){
NSError *err = nil;
CIImage *filtered = [request.sourceImage imageByClampingToExtent];
[filter setDefaults];
[filter setValue:filtered forKey:kCIInputImageKey];

[filter setValue:[NSNumber numberWithFloat:0.00] forKey:@"inputSaturation"];
[filter setValue:[NSNumber numberWithFloat:0.00] forKey:@"inputBrightness"];
[filter setValue:[NSNumber numberWithFloat:value] forKey:@"inputContrast"];

CIImage *output = [filter.outputImage imageByCroppingToRect:request.sourceImage.extent];
if (filtered)
[request finishWithImage:output context:nil];
else
[request finishWithError:err];

}];

最佳答案

当 slider 值更改时,您可以仅更改合成 block 外部的过滤器参数,而不是在 block 内部重新设置它。所以就这样做

[adjustFilter setValue:[NSNumber numberWithFloat:sender.value] forKey:@"inputContrast"];

在您的 valueChanged 回调中而不是在组合 block 中。这样,过滤器始终会获取最新值并将其用于下一个视频帧。

编辑:

以下是 View Controller 内可能实现的一些部分:

@interface DemoViewController ()

@property (strong, nonatomic) CIFilter *filter;
@property (strong, nonatomic) AVPlayer *player;
@property (strong, nonatomic) AVAsset *tempAVAsset;

@end

@implementation DemoViewController

- (void)viewDidLoad {
[super viewDidLoad];

// setup filter
self.filter = [CIFilter filterWithName:@"CIColorControls"];
[self.filter setDefaults];
[self.filter setValue:[NSNumber numberWithFloat:0.00] forKey:@"inputSaturation"];
[self.filter setValue:[NSNumber numberWithFloat:0.00] forKey:@"inputBrightness"];

AVVideoComposition *composition = [AVVideoComposition videoCompositionWithAsset:self.tempAVAsset applyingCIFiltersWithHandler:
^(AVAsynchronousCIImageFilteringRequest *request){
NSError *err = nil;
CIImage *filtered = [request.sourceImage imageByClampingToExtent];

[self.filter setValue:filtered forKey:kCIInputImageKey];
CIImage *output = [self.filter.outputImage imageByCroppingToRect:request.sourceImage.extent];
if (filtered)
[request finishWithImage:output context:nil];
else
[request finishWithError:err];
}
];

// setup player with composition
// ...
}


- (IBAction)sliderValueChanged:(UISlider*)sender
{
// get the new slider value and assign it to the filter
[self.filter setValue:[NSNumber numberWithFloat:sender.value] forKey:@"inputContrast"];
}

@end

关于ios - 如何使用 UISlider 更改视频的 CIFilter 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57820677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com