Home Features Docs Blog Support GitHub

UIImage-Picker-Controller-Delegate functions are never called


(Sameh Mahmoud) #1

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.


(Sameh Mahmoud) #2

what is wrong with this ?!


(Eugene) #3

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).


(Sameh Mahmoud) #5

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


(Sameh Mahmoud) #6
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