Follow along at https://www.hackingwithswift.com/100/swiftui/65.
This day covers Part Four of Project 13: Instafilter
in the 100 Days of SwiftUI Challenge. (Project 13 files can be found in the directory for Part One.)
It focuses on several specific topics:
- Building our basic UI
- Importing an image into SwiftUI using UIImagePickerController
- Basic image filtering using Core Image
This is just one of many cases where tying user interface controls to Combine publishers can really shine.
By tying out slider to a publisher that updates and observed-object value, we can have the slider react to updates continuously... but not too continuously! -- giving it a slight debounce so that we aren't trying to apply image processing with every microscopic drag movement.
View:
Slider(
value: $viewModel.filterIntensity,
minimumValueLabel: Text("0"),
maximumValueLabel: Text("1")
) {
Text("Intensity")
}
ViewModel ObservableObject:
private var filterIntensityPublisher: AnyPublisher<CGFloat, Never> {
$filterIntensity
.debounce(for: .milliseconds(200), scheduler: DispatchQueue.main)
.eraseToAnyPublisher()
}