每次从图库中获取照片时,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>
这是我的 View Controller 中的所有代码 我想使用 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 是否已正确连接到此 socket 。看起来它已损坏或连接到不正确的类型。
您可以通过注释掉以下行来验证问题是否存在:
confirmImage.image = selectedImage
当您在上面注释掉如果应用程序没有崩溃时,我最初的假设是正确的。
关于ios - 为什么从库中获取照片时 imagePickerController 会崩溃,但从相机中获取照片却可以正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57308045/