每次从库中获取照片时,ImagePickerController都会崩溃,并且警告将像这样显示。
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0x6000033a8280'
我正在使用UIImagePickerController
但是当我从相机中获取照片时,它可以正常工作。
我已将此代码放在info.list中
<key>NSCameraUsageDescription</key>
<string>This app wants to take pictures.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>This app wants to use your photos.</string>
这是我在viewcontroller中的所有代码
我想使用ImagePickerController从图库中获取照片,但我不知道为什么它会崩溃。
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var shopIDNumber: UILabel!
@IBOutlet weak var bgAddImage: UIView!
@IBOutlet weak var doneButton: UIButton!
@IBOutlet weak var confirmImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
tapOnImage()
shopIDNumber.layer.cornerRadius = 8
bgAddImage.layer.cornerRadius = 8
doneButton.layer.cornerRadius = 25
}
@objc func tapToImageView(sender: UITapGestureRecognizer) {
let pickerController = UIImagePickerController()
pickerController.delegate = self
pickerController.allowsEditing = true
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action) in
if UIImagePickerController.isSourceTypeAvailable(.camera) {
pickerController.sourceType = .camera
self.present(pickerController, animated: true, completion: nil)
}else{
let alert = UIAlertController(title: "Mistake", message: "Camera Not Work", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Okey", style: .default) { (action) in
}
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
}))
actionSheet.addAction(UIAlertAction(title: "Library", style: .default, handler: { (action) in
pickerController.sourceType = .photoLibrary
self.present(pickerController, animated: true, completion: nil)
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))
self.present(actionSheet, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
var selectedImageFromPicker: UIImage?
if let editedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
selectedImageFromPicker = editedImage
}else if let originalImage:UIImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
selectedImageFromPicker = originalImage
}
if let selectedImage = selectedImageFromPicker {
confirmImage.image = selectedImage
}
self.doneButton.backgroundColor = UIColor(red: 0.0, green: 182.0 / 255.0, blue: 79.0 / 255.0, alpha: 1.0)
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
func tapOnImage() {
let tapGestureToImageView = UITapGestureRecognizer(target: self, action: #selector(tapToImageView(sender:)))
tapGestureToImageView.numberOfTapsRequired = 1
confirmImage?.isUserInteractionEnabled = true
confirmImage.addGestureRecognizer(tapGestureToImageView)
}
}
最佳答案
您的代码看起来不错。最可能的问题是跟随。
@IBOutlet weak var confirmImage: UIImageView!
检查您的Interface Builder是否与该插座正确连接。看起来它已损坏或挂接到错误的类型。
您可以通过注释以下行来验证问题是否在这里:
confirmImage.image = selectedImage
当您在应用程序没有崩溃的情况下在行上方注释掉时,我原来的假设是正确的。
关于ios - 从库中获取照片但从相机中获取照片时imagePickerController为什么会崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57308045/