允许用户 select 来自相机、照片库或相册的图像并遇到我无法解决的错误
Allowing user to select image from Camera, Photo Library or Photo Albums and hit an error I cannot solve
我一直在关注 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
}
}
}
我已经回答了我自己的问题!
扩展 class ViewController 的命名有误。我的 .swift 文件被命名为其他名称。
我还从代码顶部的原始 class 移动了 UIImageViewControllerDelegate 和 UINavigatonControllerDeletage,因此它只出现在扩展中!
我一直在关注 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
}
}
}
我已经回答了我自己的问题!
扩展 class ViewController 的命名有误。我的 .swift 文件被命名为其他名称。
我还从代码顶部的原始 class 移动了 UIImageViewControllerDelegate 和 UINavigatonControllerDeletage,因此它只出现在扩展中!