I have a class where I am calling the UIImagePickerController
to select an image and return it so I can use it somewhere else in my code.
Now I set the delegate and implement the necessary function I need, but the delegate functions aren't being called.
The portion of my code that has the issue:
import UIKit
typealias PhotoTakingHelperCallback = (UIImage? -> Void)
class PhotoTakingHelper: NSObject
{
weak var viewController:UIViewController!
var callback: PhotoTakingHelperCallback
var imagePickerController: UIImagePickerController?
init(viewController:UIViewController , callback:PhotoTakingHelperCallback) {
self.viewController = viewController
self.callback = callback
super.init()
showPhotoSourceSelection()
}
func showPhotoSourceSelection() {
let alertController = UIAlertController(title: nil, message: "Where do you want to get your picture from?", preferredStyle: .ActionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
alertController.addAction(cancelAction)
let photoLibraryAction = UIAlertAction(title: "Photo from Library", style: .Default) { (action) -> Void in
self.showImagePickerController(.PhotoLibrary)
}
alertController.addAction(photoLibraryAction)
if (UIImagePickerController.isCameraDeviceAvailable(.Rear) ) {
let cameraAction = UIAlertAction(title: "Photo from Camera", style: .Default, handler: { (action) -> Void in
self.showImagePickerController(.Camera)
})
alertController.addAction(cameraAction)
}
viewController.presentViewController(alertController, animated: true, completion: nil)
}
func showImagePickerController(sourceType: UIImagePickerControllerSourceType) {
imagePickerController = UIImagePickerController()
imagePickerController!.delegate = self
imagePickerController!.sourceType = sourceType
print("Set the delegate \(imagePickerController?.delegate?.isMemberOfClass(PhotoTakingHelper))")
self.viewController.presentViewController(imagePickerController!, animated: true, completion: nil)
}
}
extension PhotoTakingHelper: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
print("HELLO?")
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
viewController.dismissViewControllerAnimated(false, completion: nil)
print("calling the callback now " )
callback(image)
print("Finished calling the callback")
}
}
I've checked iPhone: UIImagePickerControllerDelegate methods not Invoked? and a similar problem and many more but none have solved the issue.
I've realized that none of the delegate methods are invoked as when I run through the program on the simulator and choose an image in the photo library, the console only prints.
Set the delegate Optional(true)