To change the hue of multiple filters in a single UISlider (last time, last time, last time) [Duplicate]

Asked 2 years ago, Updated 2 years ago, 118 views

(One answer) 6 years ago

While the image selected from the camera roll is depicted in imageView, the function of filtering sepia or black and white by tapping the button is implemented.
In addition, we would like to implement a feature that allows users to drag the UISlider from side to side to select sepia or black and white shades.
I did coding on my own, but I don't know where to go from here.
Note: Based on the code you answered last time, we have coded it this time.
Select the image with the camera roll, tap the filter button, and then
If you drag the UISlider left or right, you get an error.
The error details are as follows:

import UIKit
classViewController:UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate{
    @IBOutlet varmainImageView: UIImageView!
    @ IBAction func pickImageFromLibrary(){
        if UIImagePickerController.isSourceTypeAvailable (.PhotoLibrary) {
            let controller = UIImagePickerController()
            controller.delegate=self
            controller.sourceType=UIImagePickerControllerSourceType.PhotoLibrary
            self.presentViewController(controller, animated:true, completion:nil)
        }
    }
    override func viewDidLoad(){
        super.viewDidLoad()
        let slider = UISlider()
        slider.frame = CGRect (x:80, y:380, width:200, height:40)
        slider.minimumValue = 0.0
        slider.maximumValue=1.0
        slider.addTarget(self, action:#selector(ViewController.sepiaFilter(_:slider:))), forControlEvents:UICControlEvents.ValueChanged)
        slider.addTarget(self, action:#selector(ViewController.monoFilter(_:slider:))), forControlEvents:UICControlEvents.ValueChanged)
        self.view.addSubview(slider)
    }
    funcimagePickerController(picker:UIImagePickerController,
    DidFinishPickingImage: UIImage, editingInfo: String: AnyObject?) {
        self.mainImageView.image=image
        (self.view.viewWithTag(1)as!UIButton).setImage(image, forState:.Normal)
        (self.view.viewWithTag(2)as!UIButton).setImage(sepiaFilter(image, slider:UISlider())), forState:.Normal)
        (self.view.viewWithTag(3) as!UIButton).setImage(monoFilter(image, slider:UISlider())), forState:.Normal)
        picker.dismissViewControllerAnimated(true, completion:nil)
    }
    @ IBAction func sepiaFilter (sender: UISlider) {
        guard let image=self.mainImageView.imageelse{
            fatalError("something is wrong with mainImageView...")
        }
        self.mainImageView.image=sepiaFilter(image, slider:sender)
    }
    func sepiaFilter (image:UIImage, slider:UISlider) - > UIImage {
        guardlet sepiaFilter=CIFilter(name: "CISepiaTone") else {
            fatalError("something is wrong with CIFilter...")
        }
        sepiaFilter.setDefaults()
        guard let ciImage: CIIimage= CIIimage(image:image) else {
        print("cannot create CIIimage...")
        return image
        }
        posterFilter.setValue(ciImage, forKey:kCIInputImageKey)
        posterFilter.setValue(slider.value, forKey: "inputLevels")
        iflet outputImage=posterFilter.valueForKey("outputImage")as? CIIimage{
            let managedImage = UIImage (CIImage: outputImage)
            return managedImage
        }
        return image
    }
    @ IBAction funcmonoFilter (sender:UISlider) {
        guard let image=self.mainImageView.imageelse{
            fatalError("something is wrong with mainImageView...")
        }
        self.mainImageView.image=monoFilter(image, slider:sender)
    }
    funcmonoFilter (image:UIImage, slider:UISlider) - > UIImage {
        guardlet monoFilter=CIFilter(name: "CIColorControls") else {
            fatalError("something is wrong with CIFilter...")
        }
        monoFilter.setDefaults()
        guard let ciImage: CIIimage= CIIimage(image:image) else {
            print("cannot create CIIimage...")
            return image
        }
        monoFilter.setValue(ciImage, forKey:kCIInputImageKey)
        monoFilter.setValue(slider.value, forKey: "inputSaturation")
        iflet outputImage=monoFilter.valueForKey("outputImage") as ? CIIimage{
            let managedImage = UIImage (CIImage: outputImage)
            return managedImage
        }
        return image
    }
    @ IBAction func filterTapped (sender: UIButton) {
        switch sender.tag {
        case1:
            mainImageView.image=(self.view.viewWithTag(1)as!UIButton).imageView?.image
        case2:
            mainImageView.image=(self.view.viewWithTag(2)as!UIButton).imageView?.image
        case3:
            mainImageView.image=(self.view.viewWithTag(3)as!UIButton).imageView?.image
        default:
            break
        }
    }
}

Enter a description of the image here

Enter a description of the image here

Enter a description of the image here

Enter a description of the image here

There were no error marks before the build.
Also, on the storyboard, you are creating a UISlider with code instead of creating a UISlider.
Could you please let me know if there are any errors or grammatical errors?

swift xcode swift2 xcode7

2022-09-30 10:50

1 Answers

As pointed out in the previous question, the format of the method responding to UIControl must be either func doSomething(), func doSomething(sender:UIButton, forEvent:UIEvent).The first argument is passed to the responding object itself (in this case, UISlider), and the second argument is passed to the event object (including touch information, etc.).According to your code, when you operate UISlider,

 func sepiaFilter (image:UIImage, slider:UISlider) - > UIImage

Specify to call (code below)

slider.addTarget(self, action:#selector(ViewController.sepiaFilter(_:slider:))), forControlEvents:UICControlEvents.ValueChanged)

Therefore, func sepiaFilter(image:UIImage, slider:UISlider) ->UIImage is called when you move the slider.
At this point, the first argument contains the UISlider object as described above.Let's do it on this line

guardlet ciImage:CIIimage=CIIimage(image:image)else{

I'm passing it to the initializer that generates the CIIimage, so I'm passing the UISlider object where I should pass it, so it crashed.
In this case, the unrecognized selector... occurs when the initializer of CIIimage tries to access the CGIimage property of UIImage, but the object of the UISlider is passed, so it accesses a property that does not exist (the = method).

What you want to do is probably to call another func sepiaFilter(sender:UISlider) in the slider operation, and then call the func sepiaFilter(image:UIImage, slider:UISlider) ->UIImage method in the line below.

slider.addTarget(self, action:#selector(ViewController.sepiaFilter(_:slider:))), forControlEvents:UICControlEvents.ValueChanged)
slider.addTarget(self, action:#selector(ViewController.monoFilter(_:slider:))), forControlEvents:UICControlEvents.ValueChanged)

If you modify the #selector(...) part, it will work properly.


2022-09-30 10:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.