Метод делегата не вызывается для UIimagePickerController
Я пытаюсь создать протокол, в котором я могу открыть UIimagePickerController с камерой или медиа-библиотекой по выбору пользователя.
вот некоторый код:
import UIKit
protocol PFImagePickerProtocol: UIImagePickerControllerDelegate,UINavigationControllerDelegate where Self: UIViewController {
func didSelectImage(image: UIImage?, error: Bool)
func didCancelledImageSelection()
}
extension PFImagePickerProtocol {
func openImageSelector(withCorp cropEnabled:Bool) {
let alertController = UIAlertController(title: "Action Sheet", message: "What would you like to do?", preferredStyle: .actionSheet)
let camera = UIAlertAction(title: "Camera", style: .default) { (action) in
self.openImagePicker(withCorp: cropEnabled, sourceType: .camera)
}
let library = UIAlertAction(title: "Photo Library", style: .default) { (action) in
self.openImagePicker(withCorp: cropEnabled, sourceType: .photoLibrary)
}
alertController.addAction(camera)
alertController.addAction(library)
self.present(alertController, animated: true, completion: nil)
}
private func openImagePicker(withCorp cropEnabled:Bool, sourceType: UIImagePickerController.SourceType) {
let pickerVc = UIImagePickerController()
pickerVc.allowsEditing = cropEnabled
pickerVc.sourceType = sourceType
pickerVc.delegate = self //is this the problem?
self.present(pickerVc, animated: true, completion: nil)
}
}
extension PFImagePickerProtocol{
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
didCancelledImageSelection()
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
didSelectImage(image: image, error: false)
return
} else {
didSelectImage(image: nil, error: true)
}
self.dismiss(animated: true, completion: nil)
}
}
как я запускаю код. функция didFinishPickingMediaWithInfo не вызывается.
Я нашел этот ответ полезным. но если есть что-то, что может решить эту проблему. пожалуйста, поделитесь здесь.
не стесняйтесь комментировать код.
2 ответа
Класс PFImagePickerManager
typealias PFImagePickerTarget = UIImagePickerControllerDelegate & UINavigationControllerDelegate
class PFImagePickerManager {
static var shared: PFImagePickerManager = PFImagePickerManager()
var target: PFImagePickerTarget!
private init() {}
func openImageSelector(target: PFImagePickerTarget, shouldCrop: Bool) {
self.target = target
let alertController = UIAlertController(title: PFConstants.PFImagePicker.actionSheetTitle, message: kEmptyStr, preferredStyle: .actionSheet)
let camera = UIAlertAction(title: PFConstants.PFImagePicker.camera, style: .default) { (action) in
self.openImagePicker(withCorp: shouldCrop, sourceType: .camera)
}
let library = UIAlertAction(title: PFConstants.PFImagePicker.photoLibrary, style: .default) { (action) in
self.openImagePicker(withCorp: shouldCrop, sourceType: .photoLibrary)
}
let cancel = UIAlertAction(title: PFConstants.PFImagePicker.cancel, style: .cancel) { (action) in
}
alertController.addAction(camera)
alertController.addAction(library)
alertController.addAction(cancel)
if let vc = target as? PFBaseViewController {
vc.present(alertController, animated: true, completion: nil)
}
}
private func openImagePicker(withCorp cropEnabled:Bool, sourceType: UIImagePickerController.SourceType) {
let pickerVc = UIImagePickerController()
pickerVc.allowsEditing = cropEnabled
pickerVc.sourceType = sourceType
pickerVc.delegate = target
if sourceType == .photoLibrary {
pickerVc.navigationBar.tintColor = UIColor.appThemePrimaryColor()
}
if let vc = target as? PFBaseViewController {
vc.present(pickerVc, animated: true, completion: nil)
}
}
}
вам нужно продлить PFImagePickerTarget
extension YourViewController: PFImagePickerTarget {
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
PFViewUtility.dispatchOnMainThread {
self.VMCreateGR.changeDatafor(.image, data: image)
self.tblInputs.reloadData()
}
} else {
self.view.makeToast("Error while selecting image. Please try again.")
}
picker.dismiss(animated: true, completion: nil)
}
}
и запустить сборщик изображений в ViewController
class AnyViewController: UIViewController {
// In some method like viewDidLoad call the below line
PFImagePickerManager.shared.openImageSelector(target: self, shouldCrop: true)
}
Пожалуйста, напишите свой код, как показано ниже:
protocol PFImagePickerProtocol {
func didSelectImage(image: UIImage?, error: Bool)
func didCancelledImageSelection()
}
И напишите ваши расширения, как показано ниже, которые содержат методы делегата:
extension YourViewController {
func openImageSelector(withCorp cropEnabled:Bool) {
let alertController = UIAlertController(title: "Action Sheet", message: "What would you like to do?", preferredStyle: .actionSheet)
let camera = UIAlertAction(title: "Camera", style: .default) { (action) in
self.openImagePicker(withCorp: cropEnabled, sourceType: .camera)
}
let library = UIAlertAction(title: "Photo Library", style: .default) { (action) in
self.openImagePicker(withCorp: cropEnabled, sourceType: .photoLibrary)
}
alertController.addAction(camera)
alertController.addAction(library)
self.present(alertController, animated: true, completion: nil)
}
private func openImagePicker(withCorp cropEnabled:Bool, sourceType: UIImagePickerController.SourceType) {
let pickerVc = UIImagePickerController()
pickerVc.allowsEditing = cropEnabled
pickerVc.sourceType = sourceType
pickerVc.delegate = self //This will set your picker delegate to view controller class & the below extension conforms the delegates.
self.present(pickerVc, animated: true, completion: nil)
}
}
extension YourViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate{
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
didCancelledImageSelection()
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
didSelectImage(image: image, error: false)
return
} else {
didSelectImage(image: nil, error: true)
}
self.dismiss(animated: true, completion: nil)
}
}
Тогда я думаю, что вы можете написать весь приведенный выше код в одном контроллере представления, таком как AbstractViewController, который является подклассом UIViewController, и все ваши другие контроллеры представления, которые имеют эту функциональность, имеют свой суперкласс как AbstractViewController.
class AbstractViewController: UIViewController {
// Do above code here
}
class OtherViewController: AbstractViewController {
// Classes that needs to implement the image picker functionality
}