UIImagePickerControllerDelegate functions are not being called - ios

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)

You're creating a PhotoTakingHelper but you're not keeping a reference to it. That is, you're doing something like this:
// In your view controller
#IBAction func pickImageButtonWasTapped() {
_ = PhotoTakingHelper(viewController: self) { image in
print("Callback!")
}
}
The UIImagePickerController keeps only a weak reference to its delegate, which is not enough to keep it alive, so you have to keep a strong reference to it. You could do it by storing the helper in an instance variable of your view controller, like this:
// In your view controller
var helper: PhotoTakingHelper?
#IBAction func pickImageButtonWasTapped() {
helper = PhotoTakingHelper(viewController: self) { image in
print("Callback!")
}
}
Or you could make the helper keep a reference to itself, like this:
// In PhotoTakingHelper
var me: PhotoTakingHelper?
init(viewController:UIViewController , callback:PhotoTakingHelperCallback) {
self.viewController = viewController
self.callback = callback
super.init()
me = self
showPhotoSourceSelection()
}
And then you need to set the reference to nil when the image picker is done, to let the helper be deallocated:
extension PhotoTakingHelper: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
viewController.dismissViewControllerAnimated(false, completion: nil)
callback(info[UIImagePickerControllerOriginalImage] as? UIImage)
me = nil
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
viewController.dismissViewControllerAnimated(false, completion: nil)
me = nil
}
}

Related

UIImagePicker init takes too long

I have an imagePicker, and when I initialize it, it takes pretty long time, it gets the screen to lag, for example, I have a screen where a user can write information and chose a picture, when I click on a button to move to that screen, it lags a bit before actually moving to that screen.
I ran the Time Profiler, and the problem with the screen seems to be the initialization of the imagePicker.
This is the imagePicker class:
import UIKit
public protocol ImagePickerDelegate: class {
func didSelect(image: UIImage?)
}
class ImagePicker: NSObject {
private let pickerController: UIImagePickerController
private weak var presentationController: UIViewController?
private weak var delegate: ImagePickerDelegate?
init(presentationController: UIViewController, delegate: ImagePickerDelegate){
self.pickerController = UIImagePickerController()
super.init()
self.presentationController = presentationController
self.delegate = delegate
self.pickerController.delegate = self
self.pickerController.allowsEditing = true
self.pickerController.mediaTypes = ["public.image"]
}
private func action(for type: UIImagePickerController.SourceType, title: String) -> UIAlertAction?{
guard UIImagePickerController.isSourceTypeAvailable(type) else { return nil}
return UIAlertAction(title: title, style: .default, handler: { [unowned self] _ in
self.pickerController.sourceType = type
self.presentationController?.present(self.pickerController, animated: true)
})
}
func present(from sourceView: UIView){
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
if let action = self.action(for: .camera, title: ImagePickerStrings.takePicture){
alertController.addAction(action)
}
if let action = self.action(for: .savedPhotosAlbum, title: ImagePickerStrings.cameraRoll) {
alertController.addAction(action)
}
alertController.addAction(UIAlertAction(title: GeneralStrings.cancel, style: .cancel, handler: nil))
self.presentationController?.present(alertController, animated: true)
}
private func pickerController(_ controller: UIImagePickerController, didSelect image: UIImage?){
controller.dismiss(animated: true, completion: nil)
self.delegate?.didSelect(image: image)
}
}
extension ImagePicker: UIImagePickerControllerDelegate{
public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.pickerController(picker, didSelect: nil)
}
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.editedImage] as? UIImage else {
return self.pickerController(picker, didSelect: nil)
}
self.pickerController(picker, didSelect: image)
}
}
And this is how I initialize it:
I have this variable inside the class:
var imagePicker: ImagePicker!
This is in the viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
imagePicker = ImagePicker(presentationController: self, delegate: self)
}
It's usually very slow on simulator and overall in debug mode.
See UIImagePickerController really slow when calling alloc init

NSObject not calling delegates of other classes

I have a fairly simply NSObject class that is what I am calling a MediaPicker. This class asks the user what type of media they want to pick and subsequently presents an UIImagePickerController or a UIDocumentPickerViewController based on their selection.
The problem I'm facing is that the UIImagePickerControllerDelegate and the UIDocumentPickerViewControllerDelegate methods are not being called.
My class is below:
import Foundation
import UIKit
import MobileCoreServices
public protocol MediaPickerDelegate: NSObjectProtocol {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any])
func imagePickerControllerDidCancel(_ picker: UIImagePickerController)
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController)
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL])
}
class MediaPicker: NSObject {
//Data
var viewController: UIViewController? = nil
var imagePicker: UIImagePickerController = UIImagePickerController()
let documentPicker = UIDocumentPickerViewController(documentTypes: [(kUTTypeImage as String), (kUTTypeMovie as String)], in: .import)
//Delegate
weak var delegate: MediaPickerDelegate? = nil
override init() {
super.init()
}
public func showMediaPicker(from presentingViewController: UIViewController) {
//Set Data
self.viewController = presentingViewController
//Create Alert
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
//Add Library Action
let libraryAction: UIAlertAction = UIAlertAction(title: "Photo & Video Library", style: .default) { (action) in
self.presentLibraryPicker()
}
libraryAction.setValue(UIImage(named: "gallery_picker_library"), forKey: "image")
libraryAction.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
alertController.addAction(libraryAction)
//Add Cloud Action
let cloudAction: UIAlertAction = UIAlertAction(title: "Other Locations", style: .default) { (action) in
self.presentDocumentPicker()
}
cloudAction.setValue(UIImage(named: "gallery_picker_cloud"), forKey: "image")
cloudAction.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
alertController.addAction(cloudAction)
//Add Cancel Action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
//Show Alert
self.viewController?.present(alertController, animated: true, completion: nil)
}
private func presentLibraryPicker() {
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .savedPhotosAlbum) ?? []
imagePicker.allowsEditing = true
self.viewController?.present(imagePicker, animated: true, completion: nil)
}
private func presentDocumentPicker() {
documentPicker.delegate = self
documentPicker.allowsMultipleSelection = false
self.viewController?.present(documentPicker, animated: true, completion: nil)
}
}
extension MediaPicker: UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
delegate?.imagePickerController(picker, didFinishPickingMediaWithInfo: info)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
delegate?.imagePickerControllerDidCancel(picker)
}
}
extension MediaPicker: UIDocumentPickerDelegate {
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
delegate?.documentPickerWasCancelled(controller)
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
delegate?.documentPicker(controller, didPickDocumentsAt: urls)
}
}
extension MediaPicker: UINavigationControllerDelegate {
//Redundant delegate implented as part of UIImagePickerControllerDelegate
}
The class calling this looks like this:
class Foo: UIViewController {
#objc func openPicker() {
let mediaPicker: MediaPicker = MediaPicker()
mediaPicker.delegate = self
mediaPicker.showMediaPicker(from: self)
}
}
I've tried making the protocol for MediaPickerDelegate both a class and NSObjectProtocol but with no luck. I also tried (thinking maybe an ARC issue from my obj-c days) making the two picker variables global and local in scope to the function but this didn't seem to change anything.
I'm also open to any feedback you have on the quality of the code that's been written/best practices that I may have missed.
Ok, so I've done some testing/tweaking and have realised that both of the document/image picker controllers are being removed from memory as soon as they're displayed. This is because they are local to the scope of the function that is calling them and the function has finished executing.
What I mean here is that in the function openPicker (from the question) the class that is calling it is doing some ARC magic and removing it as soon as it thinks the function is finished being called. Because the mediaPicker is local to openPicker and not global to class Foo it doesn't understand that it needs to keep it in memory.
The solution here is to have a global variable at a class level inside class Foo and access that inside of the function openPicker instead of initialising a new instance of MediaPicker as follows:
class Foo: UIViewController {
//Data
var mediaPicker: MediaPicker = MediaPicker()
#objc func openPicker() {
self.mediaPicker.delegate = self
self.mediaPicker.showMediaPicker(from: self)
}
}
Once I migrated to using this approach, the delegates are being called and the debugger clearly shows that the object is being retained in memory whilst the class/UIViewController is still interactively available to the user.
Have try to get permission for Photo Library(Privacy - Photo Library Usage Description) in Info.plist?

Value of type 'Binder<UIImage?>' has no member 'bind'

I want to bind my ImageView to viewModel for save my selected image to Core Data.
My code look like this:
class FoodViewModel: FoodViewModelType {
var foodImage: BehaviorRelay<UIImage?>
//... another code
}
My controller:
class NewFoodViewController: UIViewController {
#IBOutlet weak var foodImageView: UIImageView!
override func viewDidLoad() {
//... another code
self.foodImageView.rx.image.bind(to: foodViewModel.foodImage).disposed(by: self.disposeBag)
}
}
And i get error:
Value of type Binder < UIImage? > has no member 'bind'
How to save my image to Core Data with good MVVM practice?
Update
I am choose photo in view controller:
func chooseImagePickerAction(source: UIImagePickerController.SourceType) {
if UIImagePickerController.isSourceTypeAvailable(source) {
let imagePicker = UIImagePickerController()
imagePicker.sourceType = source
imagePicker.delegate = self
self.present(imagePicker, animated: true, completion: nil)
}
}
#objc func foodImageViewTapped(_ sender: AnyObject) {
let alertController = UIAlertController(title: "Photo path", message: nil, preferredStyle: .actionSheet)
let cameraAction = UIAlertAction(title: "Camera", style: .default) { (action) in
self.chooseImagePickerAction(source: .camera)
}
let photoLibAction = UIAlertAction(title: "Photo", style: .default) { (action) in
self.chooseImagePickerAction(source: .photoLibrary)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .default)
alertController.addAction(cameraAction)
alertController.addAction(photoLibAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
extension NewFoodViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let info = convertFromUIImagePickerControllerInfoKeyDictionary(info)
foodImageView.image = info[convertFromUIImagePickerControllerInfoKey(UIImagePickerController.InfoKey.originalImage)] as? UIImage
foodImageView.contentMode = .scaleAspectFill
dismiss(animated: true, completion: nil)
}
private func convertFromUIImagePickerControllerInfoKeyDictionary(_ input: [UIImagePickerController.InfoKey: Any]) -> [String: Any] {
return Dictionary(uniqueKeysWithValues: input.map {key, value in (key.rawValue, value)})
}
private func convertFromUIImagePickerControllerInfoKey(_ input: UIImagePickerController.InfoKey) -> String {
return input.rawValue
}
}
And in viewDidLoad (without image):
saveNewFoodBarButtonItem.rx.tap.subscribe(onNext: { [weak self] _ in
guard let self = self else { return }
let foodViewModel = FoodViewModel()
self.foodQuantityTypeTextField.rx.text.bind(to: foodViewModel.foodQuantityType).disposed(by: self.disposeBag)
self.foodShelfLifeTextField.rx.text.bind(to: foodViewModel.foodShelfLife).disposed(by: self.disposeBag)
self.foodCategoryTextField.rx.text.bind(to: foodViewModel.foodCategoryId).disposed(by: self.disposeBag)
self.foodQuantityTextField.rx.text.bind(to: foodViewModel.foodQuantity).disposed(by: self.disposeBag)
self.foodNameTextField.rx.text.bind(to: foodViewModel.foodName).disposed(by: self.disposeBag)
foodViewModel.saveNewFood(fridgeViewModel: self.fridgeViewModel!)
self.dismiss(animated: true)
}).disposed(by: disposeBag)
UIImageView is not bindable because it is an output view, not an input view, i.e., you push things into it, it doesn't push things out.
In order to emit an image to your view model, you need to do it the at the point where you push the image into the UIImageView.

UIImagePickerController delegate is not being called

Using the code below it opens the camera but fails to call the picker delegate method. I'm getting no error messages.
import Foundation
import UIKit
import MobileCoreServices
class RecVidController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
RecVidController.startRecord(delegate: self, sourceType: .camera)
}
static func startRecord(delegate: UIViewController & UINavigationControllerDelegate & UIImagePickerControllerDelegate, sourceType: UIImagePickerController.SourceType) {
guard UIImagePickerController.isSourceTypeAvailable(sourceType) else { return }
let mediaUI = UIImagePickerController()
mediaUI.sourceType = sourceType
mediaUI.mediaTypes = [kUTTypeMovie as String]
mediaUI.allowsEditing = true
mediaUI.delegate = delegate
delegate.present(mediaUI, animated: true, completion: nil)
}
#objc func video(_ videoPath: String, didFinishSavingWithError error: Error?, contextInfo info: AnyObject) {
let title = (error == nil) ? "Success" : "Error"
let message = (error == nil) ? "Video was saved" : "Video failed to save"
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.cancel, handler: nil))
present(alert, animated: true, completion: nil)
}
}
// MARK: - UIImagePickerControllerDelegate
extension RecVidController: UIImagePickerControllerDelegate {
private func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
dismiss(animated: true, completion: nil)
guard let mediaType = info[UIImagePickerController.InfoKey.mediaType] as? String,
mediaType == (kUTTypeMovie as String),
let url = info[UIImagePickerController.InfoKey.mediaURL] as? URL,
UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(url.path)
else { return }
// Handle a movie capture
UISaveVideoAtPathToSavedPhotosAlbum(url.path, self, #selector(video(_:didFinishSavingWithError:contextInfo:)), nil)
}
}
// MARK: - UINavigationControllerDelegate
extension RecVidController: UINavigationControllerDelegate {
}
The problem is this declaration:
private func imagePickerController(
_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
You have declared this method private, so Objective-C has no way of knowing that it exists. Thus, it can't call into it.
Basically, Cocoa looks to see whether this method is implemented, discovers that it isn't (because you've hidden it), and gives up. There's no visible penalty, because this delegate method is optional, and when it is not implemented, Cocoa dismisses the picker for you when the user is finished with it.
So just delete private and you should be good to go. This exposes the delegate method to Objective-C, and so it will be called.
(You do not have to say #objc to expose it to Objective-C, as you would if this were your own function, because you've declared we adopt UIImagePickerControllerDelegate, which is an Objective-C protocol.)
I had a similar issue when delegate methods were not called by program.
The imagePicker.delegate = self should be NOT in viewDidLoad() method but in method which opens gallery. Like this:
func openGallery()
{
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
self.present(imagePicker, animated: true, completion: nil)
}

Include UIImagePickerController in a view

I need to make a view that works like pages, with a popup in which I can choose to pick a photo from my library or create a new one.
Currently, I have a ViewController, present as Popover. In the ViewController, I inserted a ContainerView and I declared that its class is UIImageViewController. This is showing me the photo library but I don't find how to pick anything in : My ViewController presented as Popover.
When I choose a photo, nothing's happening. I've tried to put some functions
(func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!))
in my ViewController but it does not work. I've read that UIImagePickerController does not support subclassing, so how Pages do this stuff ?
Here is my ViewController code :
import UIKit
class MenuAddResources: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var newMedia: Bool?
let imagePicker = UIImagePickerController()
#IBAction func takePhoto(sender: AnyObject) {
if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
//load the camera interface
let picker : UIImagePickerController = UIImagePickerController()
picker.sourceType = UIImagePickerControllerSourceType.Camera
picker.delegate = self
picker.allowsEditing = false
self.presentViewController(picker, animated: true, completion: nil)
self.newMedia = true
}
else{
//no camera available
let alert = UIAlertController(title: NSLocalizedString("ERROR", comment: ""), message: NSLocalizedString("NO_CAMERA", comment: ""), preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .Default, handler: {(alertAction)in
alert.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
func image(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo:UnsafePointer<Void>) {
if error != nil {
let alert = UIAlertController(title: NSLocalizedString("ERROR", comment: ""), message: NSLocalizedString("IMAGE_SAVE_FAILED", comment: ""), preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .Cancel, handler: nil)
alert.addAction(cancelAction)
self.presentViewController(alert, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
self.dismissViewControllerAnimated(true, completion: { () -> Void in
})
// Let's store the image
let now:Int = Int(NSDate().timeIntervalSince1970)
let imageData = UIImageJPEGRepresentation(image, 85)
//imageData?.writeToFile(documentsDirectory + "/\(now).jpg", atomically: true)
print(imageData)
/* will do stuff with the image */
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
It appears you are using UIImagePickerViewController through a
container view and therefore the delegate isnt set , thus no callbacks
to receiving the image picked methods.
To fix this, you must override prepareForSegue in your class
and in that cast the segue.destinationViewController to your picker and set its delegate to self

Resources