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

标签 ios swift uiimagepickercontroller

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

我已经过测试了
第一步

  1. 按下拍照按钮
    • 访问摄像头
      1. 按取消我在输出面板上获取值
        • 检查图像拍照==>可选(UIImagePickerController: 0x107879a00)
      2. 按项目上的“保存照片”按钮
        我得到的值(value)为零
        • fatal error :解包可选值时意外发现 nil 2019-07-05 08:52:24.667938+0700 MyProject[2307:786209] fatal error :解包可选值时意外发现 nil

我已经过测试了
第二步

  1. 拍照

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

      Check image take photo ==> Optional(UIImagePickerController: 0x10286ca00)

我想检查相机上的案例取消并返回按项目上的保存按钮

How to fix….this case My code below

拍照

 @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

最佳答案

每当您取消图像选择器时,都会调用委托(delegate)方法 -

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

关于ios - 如何在访问相机并按相机上的取消时检查图像为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56895841/

相关文章:

ios - 在 UIPageViewController 中获取 ViewController 的容器 View

ios - 使用 UIImagePickerController 使 UIView 显示相机 View

ios - 如何计算/遍历 NSManagedObject 具有的所有属性?

ios - 在 ios Swift 中获取所有联系人?

ios - 适用于 iOS 6 的新 Facebook SDK 在 iOS 5 中存在崩溃问题

ios - swift 3 : CNcontactpicker's phonenumber return 'not fetched' in device

Swift 项目在 Facebook 登录后无法正确继续

iphone - 使用 UIImagePickerController 拍照后如何调整图片大小?

uiimagepickercontroller - iOS 11 - 始终打开照片库,即使源类型从 .photoLibrary 更改为 .camera

ios - 删除 uiwindow 的 subview ?