1

I am loading a button on right navigation item programmatically. Then i call a function to check the current image. This function basically turns on the flash but some how ots not going inside the if condition. my code is

 @objc func flashOnOff(sender: UIButton!) {
    print("Flash button press")
    //        let flashsetting = AVCapturePhotoSettings()
    guard (currentDevice?.isTorchAvailable)! else {return}


    do {
        try currentDevice?.lockForConfiguration()
        if flashButton.currentImage == UIImage(named: "FlashOffIcon.png")
        {

           flashButton.setImage(#imageLiteral(resourceName: "Flash On Icon").withRenderingMode(.alwaysOriginal), for: .normal)
            navigationItem.rightBarButtonItem = UIBarButtonItem(customView: flashButton)

            //            flashButton.clipsToBounds = true
            currentDevice?.torchMode = .on
            flashonindicator = 1
        }
        else
        {
            flashButton.setImage(#imageLiteral(resourceName: "FlashOffIcon").withRenderingMode(.alwaysOriginal), for: .normal)
            navigationItem.rightBarButtonItem = UIBarButtonItem(customView: flashButton)
            currentDevice?.torchMode = .off
            flashonindicator = 0
        }
    }
    catch{
        debugPrint(Error.self)
    }
}

Any idea?

  • set a breakpoint at `if flashButton.currentImage == UIImage(named: "FlashOffIcon.png")` and check if the condition matches? – sanjaykmwt Dec 04 '18 at 14:28
  • I Have kept a checkpoint but it's not going inside. The name of the image is the same. It's going directly to else. – Shivam Srivastava Dec 04 '18 at 14:29
  • I believe you are comparing always different instances hence the `==` cannot work. Have a look at this - possible duplicate of https://stackoverflow.com/questions/34752570/uiimage-is-equal-to – Peter Pajchl Dec 04 '18 at 14:31
  • Possible duplicate of [UIImage is equal to](https://stackoverflow.com/questions/34752570/uiimage-is-equal-to) – Peter Pajchl Dec 04 '18 at 14:37
  • Just a note: please never do like `(currentDevice?.isTorchAvailable)!`. The proper way is to unwrap the optional itself, like: `currentDevice!.isTorchAvailable` if it's not nil (or use "if let"). – Eric Aya Dec 04 '18 at 14:42

2 Answers2

0

Upto my knowledge you cannot compare two UIImage directly with == operator, try doing it in below given way and then check if it works

var data1: Data? = UIImagePNGRepresentation(image1)
var data2: Data? = UIImagePNGRepresentation(image2)
sanjaykmwt
  • 586
  • 3
  • 19
-2
guard (currentDevice?.isTorchAvailable)! else {return}

Did enter always in this return?

Btw, i suggest you to use this guard like that:

guard 
   let currentDevice = currentDevice,
   currentDevice.isTorchAvailable
else {
    return
}

Then you can simple use the constant currentDevice.XXX

Pincha
  • 93
  • 1
  • 10
  • No, it goes till the end, if and checks the image but it not going inside if condition. – Shivam Srivastava Dec 04 '18 at 14:32
  • Oh yeah, this condition will always fail, different instances. try this: if flashButton.currentImage == #imageLiteral(resourceName: "FlashOffIcon") I'm assuming that you have that image in a image set on resources – Pincha Dec 04 '18 at 14:33