简体   繁体   中英

uiimagepickercontroller didfinishpickingmediawithinfo NOT CALLED when selecting a video from the library

I am writing an app where the user can select photos and videos from the library. I want to implement my own video player when a video is selected but the app immediately starts the default video player that has the choose button.

The didfinishpickingmediawithinfo function does not get called. This only happens when a video is selected. I can display a selected photo to screen because the delegate method gets called in the case of photo selection.

Why is the delegate method for the picker not being called only when a video is selected from the library?

Code for library button click:

//Library access function displays photos and videos stored on the device
- (IBAction)selectPhoto:(UIButton *)sender {

UIImagePickerController *picker2 = [[UIImagePickerController alloc] init];
picker2.delegate = self;
picker2.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker2.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:picker2.sourceType];
picker2.allowsEditing = NO;

self.picker2 = picker2;

[self.picker dismissViewControllerAnimated:YES completion:^{
    [self presentViewController:self.picker2 animated:YES completion:NULL];}];
}

I do include the delegates in the view controller. Lots of questions I have researched point out the lack of inclusion within the header to the view controller but I assure you they are there.

Here is the code for the inclusion:

@interface ViewController : UIViewController <UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIGestureRecognizerDelegate>

I have placed NSLog statements within the delegate method and I see that they are executed in all other cases when using the picker. However, in the case of video selection the NSLog statements do not appear.

If anyone has had this problem before and come up with a solution, please share it. I have searched for many days and have not found a solution to this problem.

A person in this thread had the same issue but the problem was never solved.

Everything this question has to offer as a solution, I have implemented within my program.

I have added the following code as per requested.

The delegate method:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    NSLog(@"didfinishpicking method triggered");
    // Get the type of media selected
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    // Handling Media Capturing (Images/Videos)
    // When an image is taken
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage] && picker.sourceType == UIImagePickerControllerSourceTypeCamera) {

        // Save the taken photo to the camera roll library
        UIImage *imageTaken = [info valueForKey:UIImagePickerControllerOriginalImage];
        UIImageWriteToSavedPhotosAlbum(imageTaken, nil, nil, nil);

        // Update the library button image
        [self.imageButton setImage:imageTaken forState:UIControlStateNormal];

    }

    // When a video is taken
    else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie] && picker.sourceType == UIImagePickerControllerSourceTypeCamera) {

        // Grab the URL for the video just taken
        NSURL *newVideo = [info objectForKey:UIImagePickerControllerMediaURL];

        // Save the video to the camera roll
        UISaveVideoAtPathToSavedPhotosAlbum([newVideo path], nil, nil, nil);


    }

    // Handling Library Previewing
    // When an image is selected
    else if ([mediaType isEqualToString:(NSString *)kUTTypeImage] && picker.sourceType != UIImagePickerControllerSourceTypeCamera) {

        NSLog(@"A picture was selected from the library.");

        UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
        self.libraryImage.image = image;

        self.libraryView.translatesAutoresizingMaskIntoConstraints = YES;

        [[UIApplication sharedApplication].keyWindow addSubview: self.libraryView];

    }

    // When a video is selected
    else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie] && picker.sourceType != UIImagePickerControllerSourceTypeCamera) {
        NSLog(@"A video was selected from the library.");
    }
}

Even with the time delay as suggested picker2 is still broken. I used the following answer for delaying the call.

This was the output given when I first tried to select a video then select a photo. This was ran with a dismiss then a delay before presenting the second picker.

> 2014-12-30 00:11:57.763 Sneak[228:907] [MPAVController] Autoplay:
> Enabling autoplay 2014-12-30 00:11:57.766 Sneak[228:907]
> [MPAVController] Autoplay: Skipping autoplay, disabled (for current
> item: 0, on player: 1) 2014-12-30 00:11:57.853 Sneak[228:907]
> [MPAVController] Autoplay: Enabling autoplay 2014-12-30 00:11:58.052
> Sneak[228:907] [MPCloudAssetDownloadController] Prioritization
> requested for media item ID: 0 2014-12-30 00:11:58.897 Sneak[228:907]
> [MPAVController] Autoplay: Skipping autoplay, disabled (for current
> item: 0, on player: 1) 2014-12-30 00:11:58.911 Sneak[228:907]
> [MPAVController] Autoplay: _streamLikelyToKeepUp: 0 -> 1 2014-12-30
> 00:12:17.576 Sneak[228:907] didfinishpicking method triggered
> 2014-12-30 00:12:17.579 Sneak[228:907] A picture was selected from the
> library.

I have done more testing and have found that the delegate method is called when selecting video but only when the choose button is clicked upon the default video player display.

I am still unsure of how to get the delegate method to be called directly upon video selection from the library and bypass the default video player. Does this extra information give anyone other ideas?

Before presenting picker2 set the property picker2.allowsEditing=NO

Let me know if it works for you or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM