gpt4 book ai didi

ios - 来自 imagePicker 的图像不会显示在我的 imageView 中

转载 作者:行者123 更新时间:2023-11-28 07:37:45 25 4
gpt4 key购买 nike

我的应用程序中有一个 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)
}
}

是的 - 基本上这只是最后一种方法才是真正的问题所在!相机可以显示所有内容,但我无法显示照片。我的方法有什么问题吗?

谢谢!

最佳答案

由于您只需要呈现 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)
}
*/
}

关于ios - 来自 imagePicker 的图像不会显示在我的 imageView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52983641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com