我的应用程序中有一个viewController,它随被调用的相机一起打开。当用户拍照并按“使用照片”时,图像应显示在同一viewController的imageView中。
我知道它可能看起来像是重复的(!)-但是我在这里读到了很多有关显示imagePicker中的图像的问题,到目前为止,它们的解决方案都不适合我。
基本上,在控制台或代码中都没有错误。 -但是,当我在设备上运行该应用程序时,按“使用照片”后该应用程序将冻结,然后我必须关闭该应用程序。
我现在唯一的想法是有一些常规的隐私使用设置或类似的设置,为了显示用户的照片,我需要对其进行设置,但是在那种情况下,我不知道该使用哪种方式!
请帮助!
代码:
class CreateNewPerson: UIViewController, UITextFieldDelegate,
UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var personPhoto: UIImageView!
let imagePicker = UIImagePickerController()
override func viewDidAppear (_ animated: Bool) {
checkCameraPermissions()
super.viewDidAppear(true)
}
private func checkCameraPermissions() {
let status = AVCaptureDevice.authorizationStatus(for: .video)
switch status {
case .authorized: self.presentPicker()
case .notDetermined: self.requestAccess()
case .denied: print("Permission denied.")
case .restricted: print("Permission restricted.")
}
}
private func requestAccess() {
AVCaptureDevice.requestAccess(for: AVMediaType.video) { granted in
if !granted {
print("Error.")
} else {
self.checkCameraPermissions()
}
}
}
private func presentPicker() {
imagePicker.delegate = self
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
private func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : UIImage]) {
let chosenImage = info[UIImagePickerControllerOriginalImage]
imagePicker.delegate = self
personPhoto.contentMode = .scaleAspectFit
personPhoto.image = chosenImage
dismiss(animated: true, completion: nil)
}
}
是的-基本上,这只是问题的最后一种方法!相机会显示所有内容,但我无法显示照片。我的方法出了什么问题?
谢谢!
答案 0 :(得分:0)
由于您只需要展示iOS标准图像选择器,因此不需要AVCaptureDevice
拥有这些权限,甚至根本不需要import AVFoundation
!
class CreateNewPerson: UIViewController, UITextFieldDelegate,
UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var personPhoto: UIImageView!
let imagePicker = UIImagePickerController()
private func presentPicker() {
imagePicker.delegate = self
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
// Swift 4.2
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let chosenImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
personPhoto.contentMode = .scaleAspectFit
personPhoto.image = chosenImage
picker.dismiss(animated: true, completion: nil)
}
/*
// Swift 4.1
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
personPhoto.contentMode = .scaleAspectFit
personPhoto.image = chosenImage
picker.dismiss(animated: true, completion: nil)
}
*/
}