I figured out how to achieve this. For everyone interested, here is the code:
MyViewController.h
#pragma once
#if PLATFORM_IOS
#import <UIKit/UIKit.h>
// UIImagePickerControllerDelegate to respond to user interactions
// UINavigationControllerDelegate because we want to present the photo library modally
@interface MyViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
// function to run in iOS main thread
+ (void) runSelectPhoto;
@end
#endif
MyViewController.cpp
// ...
#if PLATFORM_IOS
#include "IOSAppDelegate.h"
#import <Foundation/Foundation.h>
#endif
#include "MyViewController.h"
#if PLATFORM_IOS
@interface MyViewController()
@end
@implementation MyViewController
+ (MyViewController*)GetDelegate
{
static MyViewController * Singleton = [[MyViewController alloc] init];
return Singleton;
}
-(void)selectPhoto
{
// create the Photo Library display object
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
// type of picker interface to be displayed by the controller
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// delegate object which receives notification when the user picks an image or exits the picker interface
picker.delegate = self;
// show object
// the UIImagePickerController is ONLY supported in portrait mode (seestackoverflow.com/a/16346664/4228316)
[[IOSAppDelegate GetDelegate].IOSController presentViewController : picker animated : NO completion : nil];
}
+(void)runSelectPhoto
{
// perform this action on the iOS main thread
[[MyViewController GetDelegate] performSelectorOnMainThread:@selector(selectPhoto) withObject:nil waitUntilDone : NO];
}
#pragma mark - Image Picker Controller delegate methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo : (NSDictionary *)info{
// get the chosen original image
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
// ...
// call C++ functions on the game thread
[FIOSAsyncTask CreateTaskWithBlock : ^ bool(void)
{
// call C++ functions
// ...
return true;
}];
// get rid of UIImagePicker
[picker dismissViewControllerAnimated : YES completion : NULL];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated : YES completion : NULL];
}
@end
#endif
The method to show the image picker is called with
#if PLATFORM_IOS
NSLog(@"Show image picker");
[MyViewController runSelectPhoto];
#endif