はじめに
CIFilterで画像の露出度を調整してみます
サンプルアプリ
実装
import SwiftUI
struct ContentView: View {
private let image = UIImage(named: "sample1")!
@State private var uiImage: UIImage?
@State private var exposure: CGFloat = 0
var body: some View {
ScrollView {
VStack {
if let uiImage {
Image(uiImage: uiImage)
.resizable()
.scaledToFit()
} else {
Image(uiImage: image)
.resizable()
.scaledToFit()
}
HStack(spacing: 15) {
Button {
applyExposureAdjust()
} label: {
Text("適用")
}
Button {
uiImage = nil
exposure = 0
} label: {
Text("リセット")
}
}
VStack {
Text("露出度: \(exposure)")
Slider(value: $exposure, in: 0...10)
}
}
.padding(.horizontal, 20)
}
}
func applyExposureAdjust() {
let context = CIContext()
let inputImage = CIImage(image: image)!
guard let filter = CIFilter(name: "CIExposureAdjust", parameters: [
kCIInputImageKey: inputImage,
kCIInputEVKey: exposure
]) else { return }
guard let outputImage = filter.outputImage else { return }
guard let cgImage = context.createCGImage(outputImage, from: outputImage.extent) else { return }
self.uiImage = UIImage(cgImage: cgImage)
}
}
おわり
最近CIFilterの記事ばっか書いてる気がしますw