允许用户从相机、照片库或相册中选择图像并遇到我无法解决的错误
Posted
技术标签:
【中文标题】允许用户从相机、照片库或相册中选择图像并遇到我无法解决的错误【英文标题】:Allowing user to select image from Camera, Photo Library or Photo Albums and hit an error I cannot solve 【发布时间】:2020-04-29 14:11:04 【问题描述】:我一直在关注 David Thorn 的视频,以允许用户使用他们的相机拍摄图像或从他们的照片库或相册中导入图像。
https://www.youtube.com/watch?v=wDzk5KDe6Uw
我一个字一个字地按照指南进行操作,但遇到了下图中的错误。我已将我的代码与本教程的 Github 中的代码进行了比较,但找不到解决方法。
Here is a screen shot of the errors(not enough reputation to embed in post)
错误是
1! “ViewController”类型的值没有成员“selectedImage”
2! nil 不能分配给类型 '(UIImagePickerController, [UIImagePickerController.InfoKey : Any]) -> ()'
2! 'nil' 不能分配给类型 '(UIImagePickerController, [UIImagePickerController.InfoKey : Any]) -> ()'
.swift 文件的类型为 UIViewController。
这是我输入的代码;
public var imagePickerController: UIImagePickerController?
public var defaultImageUrl: URL?
internal var selectedImage: UIImage?
get
return self.selectedImageView.image
set
switch newValue
case nil:
self.selectedImageView.image = nil
self.selectImageButton.isEnabled = true
self.selectImageButton.alpha = 1
self.removeImageButton.isEnabled = false
self.removeImageButton.alpha = 0.5
default:
self.selectedImageView.image = newValue
self.selectImageButton.isEnabled = false
self.selectImageButton.alpha = 0.5
self.removeImageButton.isEnabled = true
self.removeImageButton.alpha = 1
//This outlet, 'selectedImageContainer' was guessed from observing the tutorial stacked their UIImageView in a UIView.
@IBOutlet weak var selectedImageContainer: UIView!
@IBOutlet weak var selectedImageView: UIImageView!
@IBOutlet weak var selectImageButton: UIButton!
didSet
guard let button = self.selectImageButton else return
button.isEnabled = true
button.alpha = 1
@IBOutlet weak var removeImageButton: UIButton!
didSet
guard let button = self.removeImageButton else return
button.isEnabled = false
button.alpha = 0.5
override func viewDidLoad()
super.viewDidLoad()
self.selectedImageView.contentMode = .scaleAspectFit
self.selectImageButton.isEnabled = self.selectedImage == nil
self.selectImageButton.alpha = 1
@IBAction func selectImageButtonAction(_ sender: Any)
if self.imagePickerController != nil
self.imagePickerController?.delegate = nil
self.imagePickerController = nil
self.imagePickerController = UIImagePickerController.init()
let alert = UIAlertController.init(title: "Select Source Type", message: nil, preferredStyle: .actionSheet)
if UIImagePickerController.isSourceTypeAvailable(.camera)
alert.addAction(UIAlertAction.init(title: "Camera", style: .default, handler: (_) in
self.presentImagePicker(controller: self.imagePickerController!, source: .camera)
))
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary)
alert.addAction(UIAlertAction.init(title: "Photo Library", style: .default, handler: (_) in
self.presentImagePicker(controller: self.imagePickerController!, source: .photoLibrary)
))
if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum)
alert.addAction(UIAlertAction.init(title: "Saved Albums", style: .default, handler: (_) in
self.presentImagePicker(controller: self.imagePickerController!, source: .savedPhotosAlbum)
))
alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel))
self.present(alert, animated: true)
internal func presentImagePicker(controller: UIImagePickerController , source: UIImagePickerController.SourceType)
controller.delegate = self
controller.sourceType = source
self.present (controller, animated: true)
@IBAction func removeImageButtonAction(_ sender: UIButton)
self.selectedImage = nil
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else
return self.imagePickerControllerDidCancel(picker)
self.selectedImage = image
picker.dismiss(animated: true)
picker.delegate = nil
self.imagePickerController = nil
func imagePickerControllerDidCancel(_ picker: UIImagePickerController)
picker.dismiss(animated: true)
picker.delegate = nil
self.imagePickerController = nil
【问题讨论】:
【参考方案1】:我已经回答了我自己的问题!
扩展类 ViewController 命名不正确。我的 .swift 文件被命名为其他名称。
我还从代码顶部的原始类中移动了 UIImageViewControllerDelegate 和 UINavigatonControllerDeletage,因此它只出现在扩展中!
【讨论】:
以上是关于允许用户从相机、照片库或相册中选择图像并遇到我无法解决的错误的主要内容,如果未能解决你的问题,请参考以下文章
如何在 CollectionView 中显示从照片库或相机中选择的图像?画廊视图
如何在使用照片框架从照片中选择图像时在选择器中隐藏 iCloud 图像