how to check for multiple images != nil

夙愿已清 提交于 2020-12-15 06:38:25

问题


I am trying to check if multiple UIImageView != nil using if statement and && operator but the problem is that when the user pick any photo of the 3 driverPImg.image != nil && passImg.image != nil && carImg.image != nil the next code exuted and it will not check for the rest conditions

this image shows 3 images line up Horizontally all those images should be filled or not nil before the Btn change color to red but what it suppose to happen didn't happen

extension VC: UINavigationControllerDelegate, UIImagePickerControllerDelegate {
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            
        if let editedImg = info[.editedImage] as? UIImage {
            
            SelectedImg = editedImg.withRenderingMode(.alwaysOriginal)
            
        } else if let originalImg = info[.originalImage] as? UIImage {
            SelectedImg = originalImg.withRenderingMode(.alwaysOriginal)
            
        }
        
        if let image = SelectedImg {
            if pictureSelectionType == .profilePicture {
                driverPImg.image = image
                
            }else if pictureSelectionType == .idPicture{
                passImg.image = image
                
            }else if pictureSelectionType == .vhiclePicture{
                carImg.image = image
                
            }else {
                backCarImg.image = image
                
            }

            if driverPImg.image != nil && passImg.image != nil && carImg.image != nil{
                sendApplyBtn.backgroundColor = UIColor.delevareColor
            }else {
                sendApplyBtn.backgroundColor = UIColor.lightGray
            }
            
            
        }
        dismiss(animated: true, completion: nil)
    }
        
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        self.dismiss(animated: true, completion: nil)
    }
    
}

回答1:


you can check it by using your placeholder, which means if your placeholder image called "placeHolder.png"

if driverPImg.image == UIImage(named: "placeHolder.png") && passImg.image == UIImage(named: "placeHolder.png") && carImg.image == UIImage(named: "placeHolder.png") {
sendApplyBtn.backgroundColor = UIColor.lightGray

            }else {
                  sendApplyBtn.backgroundColor = UIColor.delevareColor
            }



回答2:


I used your code and created a snippet on playground. It seems to work fine, I have initialised required variables for code to work.

var btnColor = UIColor.gray

var SelectedImg: UIImage? = UIImage.init(systemName: "house")

enum PictureSelectionType {
    case profilePicture
    case idPicture
    case vhiclePicture
}

var pictureSelectionType: PictureSelectionType? = .idPicture

var driverPImg: UIImageView! = UIImageView()
var passImg: UIImageView! = UIImageView()
var carImg: UIImageView! = UIImageView()
var backCarImg: UIImageView! = UIImageView()

func imagePickerController() {
    
//    if let editedImg = info[.editedImage] as? UIImage {
//
//        SelectedImg = editedImg.withRenderingMode(.alwaysOriginal)
//
//    } else if let originalImg = info[.originalImage] as? UIImage {
//        SelectedImg = originalImg.withRenderingMode(.alwaysOriginal)
//
//    }
    
    if let image = SelectedImg {
        if pictureSelectionType == .profilePicture {
            let image = UIImage()
            driverPImg.image = image
            
        }else if pictureSelectionType == .idPicture{
            passImg.image = image
            
        }else if pictureSelectionType == .vhiclePicture{
            carImg.image = image
            
        }else {
            backCarImg.image = image
        }
        
        if driverPImg.image != nil && passImg.image != nil && carImg.image != nil{
            btnColor = UIColor.red
            print("Red color")
        }else {
            btnColor = UIColor.lightGray
            print("Gray color")
        }
    }
}

imagePickerController()
pictureSelectionType = .profilePicture
imagePickerController()
pictureSelectionType = .vhiclePicture
imagePickerController()

Make sure previous selected image is saved and not nullified for if condition to work.



来源:https://stackoverflow.com/questions/64183657/how-to-check-for-multiple-images-nil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!