gpt4 book ai didi

ios - 如何对一张图片应用滤镜?

转载 作者:行者123 更新时间:2023-11-28 13:27:57 24 4
gpt4 key购买 nike

我是 Swift 的初学者,我制作了一个应用程序“照片编辑器”。我有个问题。我需要对一张图片应用一些滤镜。我有四个 slider (棕褐色、对比度、亮度、饱和度)和一张图像。我想将所有这些过滤器应用于我的图像。

我试图改变我的代码逻辑,但我有同样的问题

@objc func sliderValueDidChange(sender: UISlider!) 

if sender.tag == 0 {
let originalCIImage = CIImage(image: self.image!)
let value = sender.value
self.sepiaLabelValue.text = String(Int(value*100))
if let sepiaCIImage = sepiaFilter(originalCIImage!, intensity: Double(value)) {
filteredImage = sepiaCIImage
self.imageView.image = UIImage(ciImage: sepiaCIImage)
}

} else if sender.tag == 1 {
let originalCIImage = CIImage(image: self.image!)
let value = sender.value
self.brightnessValue.text = String(Int(value*100))
if let brightnessCIImage = brightnessFilter(originalCIImage!, intensity: Double(value)) {
filteredImage = brightnessCIImage
self.imageView.image = UIImage(ciImage:brightnessCIImage)
}

} else if sender.tag == 2 {
let originalCIImage = CIImage(image: self.image!)
let value = sender.value
self.contrastLabel.text = String(Int(value*100))
let contrastCIImage = contrastFilter(originalCIImage!, intensity: Double(value))
self.imageView.image = UIImage(ciImage:contrastCIImage!)

} else if sender.tag == 3 {
let originalCIImage = CIImage(image: self.image!)
let value = sender.value
self.saturationValue.text = String(Int(value*100))
let saturationCIImage = saturationFilter(originalCIImage!, intensity: Double(value))
self.imageView.image = UIImage(ciImage:saturationCIImage!)
}
}


func sepiaFilter(_ input: CIImage, intensity: Double) -> CIImage?
{
let sepiaFilter = CIFilter(name:"CISepiaTone")
sepiaFilter?.setValue(input, forKey: kCIInputImageKey)
sepiaFilter?.setValue(intensity, forKey: kCIInputIntensityKey)
return sepiaFilter?.outputImage
}

func brightnessFilter(_ input: CIImage, intensity: Double) -> CIImage? {
let brightnessFilter = CIFilter(name: "CIColorControls")
brightnessFilter?.setValue(input, forKey: kCIInputImageKey)
brightnessFilter?.setValue(intensity, forKey: kCIInputBrightnessKey)
return brightnessFilter?.outputImage
}

func contrastFilter(_ input: CIImage, intensity: Double) -> CIImage? {
let contrastFilter = CIFilter(name: "CIColorControls")
contrastFilter?.setValue(input, forKey: kCIInputImageKey)
contrastFilter?.setValue(intensity, forKey: kCIInputContrastKey)
return contrastFilter?.outputImage
}

func saturationFilter(_ input: CIImage, intensity: Double) -> CIImage? {
let saturationFilter = CIFilter(name: "CIColorControls")
saturationFilter?.setValue(input, forKey: kCIInputImageKey)
saturationFilter?.setValue(intensity, forKey: kCIInputSaturationKey)
return saturationFilter?.outputImage
}

当我应用第一个过滤器然后应用下一个过滤器时,第一个过滤器的值被确定为开始状态。如何将所有滤镜应用到我的图像?

最佳答案

正如 Gigi 所说,诀窍是在值发生变化时始终对图像应用两个滤镜。这应该可以解决问题:

let colorControlsFilter = CIFilter(name: "CIColorControls")!
let sepiaFilter = CIFilter(name:"CISepiaTone")!


@objc func sliderValueDidChange(sender: UISlider!) {
switch sender.tag {
case 0:
let value = sender.value
self.sepiaLabelValue.text = String(Int(value*100))
self.sepiaFilter.setValue(value, forKey: kCIInputIntensityKey)
case 1:
let value = sender.value
self.brightnessValue.text = String(Int(value*100))
self.colorControlsFilter.setValue(value, forKey: kCIInputBrightnessKey)
case 2:
let value = sender.value
self.contrastLabel.text = String(Int(value*100))
self.colorControlsFilter.setValue(value, forKey: kCIInputContrastKey)
case 3:
let value = sender.value
self.saturationValue.text = String(Int(value*100))
self.colorControlsFilter.setValue(value, forKey: kCIInputSaturationKey)
default: pass
}

self.updateImage()
}

func updateImage() {
let originalCIImage = CIImage(image: self.image!)
// first apply color controls (contrast, brightness, saturation), then sepia
self.colorControlsFilter.setValue(originalCIImage, forKey: kCIInputImageKey)
self.sepiaFilter.setValue(self.colorControlsFilter.outputImage, forKey: kCIInputImageKey)
self.imageView.image = UIImage(ciImage: self.sepiaFilter.outputImage!)
}

关于ios - 如何对一张图片应用滤镜?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58034716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com