can someone help me to understand the correct way to switch CIFilters without restart video player?
I have a local video playing inside a view. If I tap a cell in collection view, video will change the CIFilter.
my code
let filter = CIFilter(name: "CIPhotoEffectNoir")!
let asset = AVAsset(url: fooURL)
let item = AVPlayerItem(asset: asset)
item.videoComposition = AVVideoComposition(asset: asset, applyingCIFiltersWithHandler: { request in
let source = request.sourceImage.clampedToExtent()
filter.setValue(source, forKey: kCIInputImageKey)
let output = filter.outputImage
request.finish(with: output!, context: nil)
})
player = AVPlayer(playerItem: item)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = videoViewDetail.bounds
videoViewDetail.layer.addSublayer(playerLayer)
player?.play()
code works great and effect is correct apply
but i have a collectionview with many CIFilters. On tap to a cell i cannot find the way to switch the filter associated to video. If i recreate a new player with the new filter and I add it as substitute to the current "addsublayer" the player will restart video.
@IBAction func tap(_ sender: UITapGestureRecognizer) {
let location = sender.location(in: self.collectionView)
let indexPath = self.collectionView.indexPathForItem(at: location)
if let index = indexPath {
// code to switch to another CIFilter like for example "CISepiaTone"
}
}
What is the best way to change CIFilters to the playing video without restart the video Is it possible to save the video with the new filter?
Thanks!