Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import SwiftUI
import AVKit
import CoreImage
import CoreImage.CIFilterBuiltins
struct ContentView: View {
@State private var currentFilter = 0
var filters : [CIFilter?] = [nil, CIFilter.sepiaTone(), CIFilter.pixellate(), CIFilter.comicEffect()]
let player = AVPlayer(url: Bundle.main.url(forResource: "tennis", withExtension: "mp4")!)
var body: some View {
VideoPlayer(player: player)
player.currentItem!.videoComposition = AVVideoComposition(asset: player.currentItem!.asset, applyingCIFiltersWithHandler: { request in
if let filter = self.filters[currentFilter]{
let source = request.sourceImage.clampedToExtent()
filter.setValue(source, forKey: kCIInputImageKey)
if filter.inputKeys.contains(kCIInputScaleKey){
filter.setValue(30, forKey: kCIInputScaleKey)
let output = filter.outputImage!.cropped(to: request.sourceImage.extent)
request.finish(with: output, context: nil)
request.finish(with: request.sourceImage, context: nil)
Picker(selection: $currentFilter, label: Text("Select Filter")) {
ForEach(0..<filters.count) { index in
Text(self.filters[index]?.name ?? "None").tag(index)
Text("Value: \(self.filters[currentFilter]?.name ?? "None")")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment