Cristian
Cristian

Reputation: 495

How to store an instance of UIImage in a var in iOS?

I have a filter function in ViewControlller.swift that filters the image of the photoImageView, which is also stored in ViewControlller.swift. The problem is that the color filter is applying to the result of the previous filtered image instead of the original image. How can I store photoImageView in an originalImage var to apply the filter to that instead?

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    //MARK: Properties
    @IBOutlet weak var photoImageView: UIImageView!

    @IBAction func slideFilter(_ sender: UISlider) {

        var hueFilter = Double(sender.value)
        hueFilter = hueFilter.rounded(toPlaces: 2)

        //create image to filter
        let inputImage = CIImage(image: photoImageView.image!)

        //Create a random color to apply
        let randomColor = [kCIInputAngleKey: hueFilter]

        //Apply filter to image
        let filteredImage = inputImage!.applyingFilter("CIHueAdjust", parameters: randomColor)     

        let renderImage = context.createCGImage(filteredImage, from: filteredImage.extent)

        // Reflect the change back in the interface
        photoImageView.image = UIImage(cgImage: renderImage!)
    }
}

When I try to store an instance of the variable photoImageView in ViewController, I get this error Cannot use instance member 'photoImageView' within property initializer; property initializers run before 'self' is available

Upvotes: 0

Views: 806

Answers (2)

Nilanshu Jaiswal
Nilanshu Jaiswal

Reputation: 1693

You can't store an instance of the variable photoImageView in the ViewController. You can, however, store an instance of the variable photoImageView within a method like viewDidLoad().

Just use a breakpoint after the line:

let inputImage = CIImage(image: photoImageView.image!)

See what is the value of the inputImage. Is this the image you want to operate upon?

Because if the inputImage is not correct, the final result will also be incorrect.

I hope it helps.

Upvotes: 1

Cristian
Cristian

Reputation: 495

I got it!

You simply just create the inputImage variable outside of any method in ViewController. That way any methods can call it.

Set the value for inputImage in viewDidLoad()(and any place where you'll be changing the image). So the filter function can access the image.

End result:

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    //MARK: Properties
    @IBOutlet weak var photoImageView: UIImageView!

    var inputImage:CIImage? 


    override func viewDidLoad() {
    super.viewDidLoad()
        inputImage = CIImage(image: photoImageView.image!)


    }


    @IBAction func slideFilter(_ sender: UISlider) {

        var hueFilter = Double(sender.value)
        hueFilter = hueFilter.rounded(toPlaces: 2)

        //Create a random color to apply
        let randomColor = [kCIInputAngleKey: hueFilter]

    //Apply filter to image
    let filteredImage = inputImage!.applyingFilter("CIHueAdjust", parameters: randomColor)     

    let renderImage = context.createCGImage(filteredImage, from: filteredImage.extent)

        // Reflect the change back in the interface
        photoImageView.image = UIImage(cgImage: renderImage!)
    }
}

Upvotes: 1

Related Questions