UIImagePickerController didFinishPickingMediaWithInfo not being called when cameraOverlyView property is assigned

陌路散爱 提交于 2019-12-21 22:49:39

问题


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.


    回答1:


    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.




    回答2:


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



    来源:https://stackoverflow.com/questions/30919230/uiimagepickercontroller-didfinishpickingmediawithinfo-not-being-called-when-came

    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!