I set up a UIImagePickerController
modal view to record a video, and once the user chooses "Use Video" it dismisses the view controller fine and does exactly what I'd like. However, as soon as I add a cameraOverlayView, which is just a UIView with nothing special going on, didFinishPickingMediaWithInfo
is never called. Even if I put an NSLog on the first line of that function, I don't see any output. More oddly still, UIImagePickerControllerDidCancel
still gets called when the user presses the Cancel button.
Prior suggestions on SO do not seem helpful as I have set the delegate and set the editing properties accordingly unlike in these popular posts:
Here's the code to launch the UIImagePickerController (taken from a Ray Wenderlich tutorial):
-(BOOL)startCameraControllerFromViewController:(UIViewController*)controller
usingDelegate:(id )delegate {
if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO)
|| (delegate == nil)
|| (controller == nil)) {
return NO;
}
if(![delegate conformsToProtocol:@protocol(UIImagePickerControllerDelegate) ]) {
NSAssert(nil, @"delegate muste conforms to UIImagePickerControllerDelegate protocol");
}else{
NSLog(@"delegate does conform to protocol");
}
CGRect screenRect = [[UIScreen mainScreen]bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
CameraOverlay *overlay = [[CameraOverlay alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraUI.delegate = delegate;
cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
cameraUI.allowsEditing = NO;
//if I comment out the line below, everything works fine.
//but if I don't comment this out, didFinishPickingMediaWithInfo is never called
cameraUI.cameraOverlayView = overlay;
[controller presentModalViewController: cameraUI animated: YES];
return YES;
}
Here's the part of didFinishPickingMediaWithInfo
:
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(@"did finish picking media with info");
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:NO];
// I also tried [picker dismissModalViewControllerAnimated:NO];
//despite it not being its own delegate, but in any case
//this did not change the result
...
}
I added the valid delegate check following Andrey's advice, but now I always see "delegate does conform to protocol" regardless of whether the overlay is in and didFinishPickingMediaWithInfo
is called.
Any suggestions for what to try or errors in the code? Thanks for taking a look.
Here's what finally worked: set
userInteractionEnabled
toFALSE
for the overlay view.I wish I knew whether this was a bug or a feature. I looked at a few tutorials for creating overlay views, and none mentioned setting this property. If anyone can fully explain why this made the difference, I'd like to mark a more complete answer than this.
@sunny finally correct solution! userInteractionEnabled = FALSE did call didFinishPickingMediaWithInfo