UIImage-Picker-Controller-Delegate functions are never called

Hi
I’m trying to use UIImagePickerController to pick an image but i’m getting the following output :

[discovery] errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 “query cancelled” UserInfo={NSLocalizedDescription=query cancelled}

Code :

		PHPhotoLibrary.requestAuthorization(new PHPhotoLibrary.Block_requestAuthorization() {
			@Override
			public void call_requestAuthorization(@NInt long code) {
				if(code == PHAuthorizationStatus.Authorized){
					UIImagePickerController imagePickerController = UIImagePickerController.alloc().init();
					imagePickerController.setAllowsEditing(true);
					imagePickerController.setSourceType(UIImagePickerControllerSourceType.PhotoLibrary);
					imagePickerController.setEditingAnimated(true , true);
					imagePickerController.setDelegate(new UIImagePickerControllerDelegate() {
						@Override
						public void imagePickerControllerDidFinishPickingMediaWithInfo(UIImagePickerController picker, NSDictionary<String, ?> info) {
							String filePath = info.valueForKey("UIImagePickerControllerImageURL").toString();
							filePath = filePath.substring(8);
							//UIImagePickerControllerReferenceURL
							//UIImagePickerControllerImageURL
							File file = new File(filePath);
							Foundation.NSLog("selected file path = '" + filePath + "' size =" + file.length());
							picker.dismissViewControllerAnimatedCompletion(true,null);
						}

						@Override
						public void imagePickerControllerDidCancel(UIImagePickerController picker) {
							picker.dismissViewControllerAnimatedCompletion(true,null);
						}
					});
					UIViewController controller = UIApplication.sharedApplication().delegate().window().rootViewController();
					controller.presentViewControllerAnimatedCompletion(imagePickerController , true , null);
				}
			}
		});

Info.plist :

	<key>NSPhotoLibraryUsageDescription</key>
	<string>You can select photos to attach to reports.</string>

This shows the image picker, but after i select an image, the output mentioned above occurs.

what is wrong with this ?!

It’s because you used new UIImagePickerControllerDelegate(). The instance has to be a proper ObjC bridge class, initialized with UIImagePickerControllerDelegate.alloc().init().

Usually it is simpler to get you outer class implement UIImagePickerControllerDelegate and then you’d just use imagePickerController.setDelegate(OuterController.this).

1 Like

I extended the class NSObject and I called “alloc().init()” and I got the same result :disappointed_relieved:

public class UIPickerDelegator extends NSObject  implements UIImagePickerControllerDelegate {
    
    protected UIPickerDelegator() {
        super(null);
    }
    
	
	@Override
     public void imagePickerControllerDidFinishPickingMediaWithInfo(UIImagePickerController picker, NSDictionary<String, ?> info) {
         
		 String filePath = info.valueForKey("UIImagePickerControllerImageURL").toString();
         filePath = filePath.substring(8);
         //UIImagePickerControllerReferenceURL
         //UIImagePickerControllerImageURL
         File file = new File(filePath);
         Foundation.NSLog("selected file path = '" + filePath + "' size =" + file.length());
         
         picker.dismissViewControllerAnimatedCompletion(true,null);
         
     }
     @Override
     public void imagePickerControllerDidCancel(UIImagePickerController picker) {
         picker.dismissViewControllerAnimatedCompletion(true,null);
     }
}

BTW, it is a libgdx app

Hi, were you able to fix the issue?

please try like this:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
profileImage.image = image
self.dismiss(animated: true, completion: nil)
}