2

I'm doing an application "Photo Editor". I'm beginner in swift. I have a problem with my sliders. They don't work as I need. May be someone know, how I can resolve this problem?

Sliders don't work correctly and I need help with it. I don't know, why it's work like that.

import UIKit

class ViewControllerFilters: UIViewController {

@IBOutlet weak var imageView: UIImageView!
var image: UIImage? = nil
var filter = CIFilter(name: "CISepiaTone")
var filteredImage: CIImage? = nil


@IBOutlet weak var slider: UISlider!
let context = CIContext()

@IBOutlet weak var slider2: UISlider!
@IBOutlet weak var slider3: UISlider!

@IBOutlet weak var slider4: UISlider!

override func viewDidLoad() {
    super.viewDidLoad()


    self.imageView.image = image
    self.slider.addTarget(self, action: #selector(sliderValueDidChange(sender:)), for: .valueChanged )
    self.slider2.addTarget(self, action: #selector(sliderValueDidChange(sender:)), for: .valueChanged)
    self.slider3.addTarget(self, action: #selector(sliderValueDidChange(sender:)), for: .valueChanged)
    self.slider4.addTarget(self, action: #selector(sliderValueDidChange(sender:)), for: .valueChanged)


}


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




@objc func sliderValueDidChange(sender: UISlider!) {
    switch sender.tag {
    case 0:
        let value = sender.value
        self.sharp.setValue(value, forKey: kCIInputSharpnessKey)
    case 1:
        let value = sender.value
        self.colorControlsFilter.setValue(value, forKey: kCIInputBrightnessKey)
    case 2:
        let value = sender.value
        self.colorControlsFilter.setValue(value, forKey: kCIInputContrastKey)
    case 3:
        let value = sender.value
        self.colorControlsFilter.setValue(value, forKey: kCIInputSaturationKey)
    default:
        print("no such elements")
    }

    self.updateImage()
}

func updateImage() {
    let originalCIImage = CIImage(image: self.image!)
    self.colorControlsFilter.setValue(originalCIImage, forKey: kCIInputImageKey)

    self.sharp.setValue(self.colorControlsFilter.outputImage, forKey: kCIInputImageKey)
    self.imageView.image = UIImage(ciImage: self.sharp.outputImage!)

}

I need a smoothly moving of my sliders, because it's impossible to work with it.

  • welcome to the stackoverflow. what do you mean by not smooth? is it laggy? – Soroush Sep 25 '19 at 06:45
  • yes. The moving of sliders comes in maybe 5 seconds after my mouse click – kosya_kosia Sep 25 '19 at 07:01
  • comment `self.updateImage()` in `sliderValueDidChange` function. If the problem is solved, probably you should revise that function since it is heavy in case of allocating resources. – Soroush Sep 25 '19 at 07:10
  • okey. It solved this problem, but it function is very important, and if it comment the code it doesn't work as I need. Where I can revise my function? – kosya_kosia Sep 25 '19 at 08:03
  • Creating a `CIImage` from `UIImage` may require extra time to copy the image data from memory to GPU memory. – Soroush Sep 25 '19 at 08:16
  • define your `originalCIImage` somewhere outside of this function since it is constant and check if it works. – Soroush Sep 25 '19 at 08:17
  • no, the same problem again... – kosya_kosia Sep 25 '19 at 08:24
  • Are you testing on Simulator? It's not very good with image processing performance and you should always test on device. Aside from that you can try to wrape the code inside `updateImage` inside block like this: `DispatchQueue.global(qos: .background).async { }`. This will perform the operations in the block in on a background there which prevents blocking the UI. – Frank Rupprecht Sep 26 '19 at 06:17
  • Okey. I did like that. func updateImage() { DispatchQueue.global(qos: .background).async { let originalCIImage = CIImage(image: self.image!) ... } } But now the app doesn't work correctly and also there're a warning "UIImageView.image must be used from main thread only – kosya_kosia Sep 26 '19 at 07:45

1 Answers1

-1

you will receive valueChanged event only when the user stops moving the slider.

slider.isContinuous = false
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 16 '22 at 15:03