UIImagePickerController didFinishPickingMediaWithI

2019-04-15 10:44发布

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:

  • uiimagepickercontroller didfinishpickingmediawithinfo NOT CALLED when selecting a video from the library
  • UIImagePickerController didFinishPickingMediaWithInfo not being called
  • 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.

    2条回答
    孤傲高冷的网名
    2楼-- · 2019-04-15 10:55

    Here's what finally worked: set userInteractionEnabled to FALSE 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.

    查看更多
    淡お忘
    3楼-- · 2019-04-15 11:14

    @sunny finally correct solution! userInteractionEnabled = FALSE did call didFinishPickingMediaWithInfo

    查看更多
    登录 后发表回答