如何在访问相机时检查图像为零并在相机上按取消
Posted
技术标签:
【中文标题】如何在访问相机时检查图像为零并在相机上按取消【英文标题】:How to to check image nil when access to camera and press cancel on camera 【发布时间】:2019-07-05 02:28:34 【问题描述】:访问相机时如何检查图像为零并在相机上按取消
我接受了测试 第一步
-
按下拍照按钮
访问摄像头
-
按取消我在输出面板上得到值
检查图像拍照 ==> 可选(UIImagePickerController: 0x107879a00)
在项目上按保存按钮照片
我的价值为零
致命错误:在展开可选值时意外发现 nil 2019-07-05 08:52:24.667938+0700 MyProject[2307:786209] 致命错误:在展开可选值时意外发现 nil
我接受了测试 第二步
拍照
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
【问题讨论】:
【参考方案1】:每当您取消图像选择器时,都会调用一个委托方法 -
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
【讨论】:
【参考方案2】:当您点击取消图像选择器时,将调用以下委托方法:
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
希望对你有帮助
【讨论】:
以上是关于如何在访问相机时检查图像为零并在相机上按取消的主要内容,如果未能解决你的问题,请参考以下文章