UIImagePickerControllerDelegate 未被调用,所选图像从未显示
Posted
技术标签:
【中文标题】UIImagePickerControllerDelegate 未被调用,所选图像从未显示【英文标题】:UIImagePickerControllerDelegate not being called, selected image never displayed 【发布时间】:2021-05-14 13:53:44 【问题描述】:我被难住了。一切正常,但不是。我可以调用相机界面并拍摄或选择一张照片,但 UIImagePickerControllerDelegate 从未被调用(我已经很好地放置了两个打印语句,永远不会被打印)。一切都在 IB 中正确连接,或者我相信它是正确的(我已经为它做好了充分的准备,所以我错过了一些完全次要的东西)。有谁愿意用一双漂亮的眼睛仔细检查一下,看看我是不是只见树木不见森林?
提前致谢。
import UIKit
import Foundation
class afterPhotoViewController: UIViewController
//MARK: - IBOutlet Properties
@IBOutlet weak var afterImageView: UIImageView!
@IBOutlet weak var aftervwImage: UIView!
@IBOutlet weak var cameraBtnPressed: UIButton!
//MARK: - Useful Variables
let afterImagePicker = UIImagePickerController()
var hud = HKProgressHUD()
var localTextFilePath : URL!
var isImageSetted = false
//MARK: - App Delegates
var isMain = false
var originFrame = CGRect()
override func viewDidLoad()
super.viewDidLoad()
// Init UI Components
self.initUI()
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
//MARK: - UI Init
func initUI()
// Set Navigation Bar
let backButton = UIBarButtonItem(title: "", style: .plain, target: navigationController, action: nil)
navigationItem.leftBarButtonItem = backButton
self.title = "\(h_proposalNumberStr)-\(h_itemIndex)"
// Set ImageView
self.afterImageView.image = #imageLiteral(resourceName: "ic_placeholder")
self.afterImageView.clipsToBounds = false
cameraBtnPressed.layer.cornerRadius = 15
cameraBtnPressed.layer.borderColor = UIColor.lightGray.cgColor
cameraBtnPressed.layer.borderWidth = 1
self.isImageSetted = false
// self.tableView.reloadData()
//MARK: - UI Actions
@IBAction func cameraBtnPressed(_ sender: Any)
//Create the AlertController and add Its action like button in Actionsheet
let actionSheetController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let cancelActionButton = UIAlertAction(title: "Cancel", style: .cancel) _ in
actionSheetController.addAction(cancelActionButton)
let chooseLibraryActionButton = UIAlertAction(title: "Choose from Library", style: .default)
_ in
self.isMain = true
self.afterImagePicker.allowsEditing = true
self.afterImagePicker.sourceType = .photoLibrary
self.afterImagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
self.present(self.afterImagePicker, animated: true, completion: nil)
actionSheetController.addAction(chooseLibraryActionButton)
let takePhotoActionButton = UIAlertAction(title: "Take a Photo", style: .default)
_ in
if UIImagePickerController.isSourceTypeAvailable(.camera)
self.isMain = true
self.afterImagePicker.allowsEditing = false
self.afterImagePicker.sourceType = .camera
self.afterImagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .camera)!
self.afterImagePicker.showsCameraControls = true
self.present(self.afterImagePicker, animated: true, completion: nil)
else
Helper.showMessage(vc: self, title: CONSTANTS.APPINFO.APP_NAME, bodyStr: "No camera available.")
actionSheetController.addAction(takePhotoActionButton)
self.present(actionSheetController, animated: true, completion: nil)
//MARK: - UIImagePickerControllerDelegate
extension afterPhotoViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate
func imagePickerControllerDidCancel(_ picker: UIImagePickerController)
print("called")
dismiss(animated: true, completion: nil)
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
print("called2")
//getting actual image
var image = info[UIImagePickerControllerEditedImage] as? UIImage
if image == nil
image = info[UIImagePickerControllerOriginalImage] as? UIImage
afterImageView.image = image
// Set Imageview Corner Radius
afterImageView.layer.cornerRadius = 5
afterImageView.clipsToBounds = true
self.isImageSetted = true
picker.dismiss(animated: true, completion: nil)
【问题讨论】:
未设置委托。在 viewDidLoad() 中设置 afterImagePicker.delegate = self 呃,插入大量 ::facepalm:: 【参考方案1】:@raja Kishan 发现了我忘记设置委托的愚蠢简单错误。
现在,手掌表情符号在哪里。
【讨论】:
【参考方案2】:您必须指定UIImagePickerController
的委托是self
,以便触发事件。
在你的情况下:
afterImagePicker.delegate = self
【讨论】:
以上是关于UIImagePickerControllerDelegate 未被调用,所选图像从未显示的主要内容,如果未能解决你的问题,请参考以下文章