如何检查以编程方式加载的导航栏按钮上的图像

时间:2018-12-04 14:25:59

标签: ios swift if-statement flash

我正在以编程方式在右侧导航项上加载按钮。然后我调用一个函数来检查当前图像。该功能基本上打开了闪光灯,但有些条件不能在if条件下进行。 我的代码是

 @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)
    }
}

有什么主意吗?

2 个答案:

答案 0 :(得分:0)

据我所知,您无法使用==运算符直接比较两个UIImage,请尝试按以下给定方式进行操作,然后检查其是否有效

var data1: Data? = UIImagePNGRepresentation(image1)
var data2: Data? = UIImagePNGRepresentation(image2)

答案 1 :(得分:-2)

guard (currentDevice?.isTorchAvailable)! else {return}

总是在此回车中输入吗?

顺便说一句,我建议您像这样使用这个警卫:

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

然后,您可以简单地使用常量currentDevice.XXX