访问相机时如何检查图像为零并按相机上的“取消”

时间:2019-07-05 02:28:34

标签: ios swift uiimagepickercontroller

访问相机时如何检查图像为零并按相机上的取消键

我已经过测试
第一步

  1. 按下拍照按钮
    • 访问摄像头
      1. 按取消,我将在输出面板上获得价值
        • 检查图像拍照==>可选(UIImagePickerController:0x107879a00)
      2. 在项目上按保存按钮照片
        我得到零价值
        • 致命错误:展开一个可选值时意外发现nil 2019-07-05 08:52:24.667938 + 0700 MyProject [2307:786209]致命错误:展开一个可选值时意外发现nil

我已经过测试
第二步

  1. 拍照

    2.按相机上的使用照片
    • 拍照以在相机上使用照片
        

      检查图像拍照==>可选(UIImagePickerController:0x10286ca00)

我想检查相机上的情况是否取消,然后按项目上的保存按钮返回

  

如何解决…这种情况   我的下面的代码

拍照

 @IBAction func takePhotoReturnOfSealButton(_ sender: UIButton) {
        imagePickerStoreListReturnSealLock = UIImagePickerController()
        imagePickerStoreListReturnSealLock.delegate = self
        imagePickerStoreListReturnSealLock.sourceType = .camera
        present(imagePickerStoreListReturnSealLock,animated: true, completion: nil)
        print("Check image take photo ==> \(String(describing: imagePickerStoreListReturnSealLock))")
    }//takePhotoReturnOfSealButton

保存照片

@IBAction func saveImageToDevice(_ sender: UIButton) {
        //Save photo to device not success have nil value
        if(imagePickerStoreListReturnSealLock == nil){
            showAlert(title: "No Have Photo", message: "Please take photo")
            print("Check image cannot save ==> \(String(describing: imagePickerStoreListReturnSealLock))")
        }else{
//          Save photo to device success
            UIImageWriteToSavedPhotosAlbum(showImageTakePhotoReturnOfSeal.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
            print("Check image Save to device ==> \(String(describing: showImageTakePhotoReturnOfSeal))")
        }
    }//saveImageToDevice

2 个答案:

答案 0 :(得分:0)

每当取消图像选择器时,就会调用一个委托方法-

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    //Perform your action when cancel has been pressed
}

拾取图像后将调用此方法-

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
   if picker == imagePicker {
        //Save your image
        imagePicker.dismiss(animated: true, completion: nil)
   }
}

只需记住在上面进行定义-

var imagePicker = UIImagePickerController()

和在viewDidLoad

imagePicker.delegate = self

答案 1 :(得分:0)

单击取消图像选择器时,将调用以下委托方法:

public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }

当您选择图像时,将调用以下委托方法:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            let image : UIImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
            picker.dismiss(animated: true, completion: nil)
            UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)//to save image in saved photo album
        }

希望这会有所帮助