Important: Please read the Qt Code of Conduct -

[SOLVED] Use ios UIImagePickerController to picking a photo in qt5.3

  • Hi, I want to implement an application for ios. I've used qt5.3 on mac. I want to defined user interface in qt. I want to have a button on ui. when cliking on that, ios photo library will be opened and then user can be able to select a photo. I know that i must use UIImagePickerController. but always i get this error and photo library is not opened.
    this error:
    Warning: Attempt to present <UIImagePickerController: 0xa8c4520> on <APLViewController: 0xa9eaa90> whose view is not in the window hierarchy!

    here is my code:
    //.mm file

    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import <Foundation/NSString.h>

    #import "mainwindow.h"
    #include <QDebug>

    @interface APLViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
    @property (nonatomic) UIImagePickerController *imagePickerController;

    @implementation APLViewController
    MainWindow *mWindowInstance;

    • (int)showImagePickerForPhotoPicker:(MainWindow *)myInstance
      [self showImagePickerForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];

      return 5;

    • (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType
      NSLog(@"start of showImagePickerForSourceType");

      UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
      imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
      imagePickerController.sourceType = sourceType;
      imagePickerController.delegate = (id)self;

      self.imagePickerController = imagePickerController;
      [self presentViewController:self.imagePickerController animated:YES completion:nil];

      NSLog(@"end of showImagePickerForSourceType");

    // This method is called when an image has been chosen from the library or taken from the camera.

    • (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
      UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
      [self finishAndUpdate];

    • (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
      [self dismissViewControllerAnimated:YES completion:NULL];

    void MainWindow::bridgeMethod(void*context, MainWindow *qIns)
    mWindowInstance = qIns;
    context = [[APLViewController alloc] init];
    [(id) context showImagePickerForPhotoPicker:qIns];


    I know the problem is in this line:
    [self presentViewController:self.imagePickerController animated:YES completion:nil];
    how can i handle that? please help me
    Thanks in advance...

  • You have to use the root controller and show the UIPickerImageController on that.
    For doing so, you need to use some internal Qt Api:

    • first, in the .pro declare you want to use private headers:
      QT += gui-private
    • then, in your .mm code get the pointer to the root view controller of the Qt app:
      UIView *view = static_cast<UIView >( QGuiApplication::platformNativeInterface()->nativeResourceForWindow("uiview",quickView) );
      rootCtrl = [[view window] rootViewController];
    • and now, you can show the UIImagePickeController:
      [rootCtrl presentViewController:imagePickerController animated:YES completation:nil]

  • thank you very much. I will try with your guide.

Log in to reply