如何使用AVFoundation框架创建和添加像Instagram这样的videofilter – Swift编程
我正在使用Swift编程处理AVFoundation框架。 有人可以帮我找一些教程或链接或代码片段来应用类似于Instagram的videofilter。
我正在研究iOSvideo创建器类型的应用程序,它记录video,后来我可以应用filter到video。
提前致谢。
您应该查看Apple的示例项目RosyWriter。 它是您想要实现的目标的一个很好的例子。
此外,您还可以查看GLImageProcessing示例项目。
https://developer.apple.com/library/ios/samplecode/GLImageProcessing/Introduction/Intro.html
希望有所帮助!
从iOS 9.0开始,您可以使用AVVideoComposition逐帧应用核心图像滤镜。
let filter = CIFilter(name: "CIGaussianBlur")! let composition = AVVideoComposition(asset: asset, applyingCIFiltersWithHandler: { request in // Clamp to avoid blurring transparent pixels at the image edges let source = request.sourceImage.imageByClampingToExtent() filter.setValue(source, forKey: kCIInputImageKey) // Vary filter parameters based on video timing let seconds = CMTimeGetSeconds(request.compositionTime) filter.setValue(seconds * 10.0, forKey: kCIInputRadiusKey) // Crop the blurred output to the bounds of the original image let output = filter.outputImage!.imageByCroppingToRect(request.sourceImage.extent) // Provide the filter output to the composition request.finishWithImage(output, context: nil) // Clamp to avoid blurring transparent pixels at the image edges let source = request.sourceImage.clampedToExtent() filter.setValue(source, forKey: kCIInputImageKey) //Vary filter parameters based on video timing let seconds = CMTimeGetSeconds(request.compositionTime) filter.setValue(seconds * 10.0, forKey: kCIInputRadiusKey) // Crop the blurred output to the bounds of the original image let output = filter.outputImage!.cropped(to: request.sourceImage.extent) request.finish(with: output, context: nil) })
现在我们可以使用之前创建的资产创建AVPlayerItem并使用AVPlayer播放它
let playerItem = AVPlayerItem(asset: asset) playerItem.videoComposition = composition let player = AVPlayer(playerItem: playerItem) player.play()
核心图像filter逐帧实时添加。 您还可以使用AVAssetExportSession类导出video。
这里是WWDC 2015的精彩介绍: https : //developer.apple.com/videos/play/wwdc2015/510/? time = 1222