从库中选择视频时未调用 uiimagepickercontroller didfinishpickingmediawithinfo

Posted

技术标签:

【中文标题】从库中选择视频时未调用 uiimagepickercontroller didfinishpickingmediawithinfo【英文标题】:uiimagepickercontroller didfinishpickingmediawithinfo NOT CALLED when selecting a video from the library 【发布时间】:2014-12-30 00:52:43 【问题描述】:

我正在编写一个应用程序,用户可以在其中从库中选择照片和视频。我想在选择视频时实现自己的视频播放器,但应用程序会立即启动具有选择按钮的默认视频播放器。

没有调用 didfinishpickingmediawithinfo 函数。这仅在选择视频时发生。我可以将选定的照片显示到屏幕上,因为在照片选择的情况下会调用委托方法。

为什么只有从库中选择视频时才调用选择器的委托方法?

库按钮点击代码:

//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];];

我确实在视图控制器中包含了委托。我研究过的许多问题都指出视图控制器的标题中缺少包含,但我向你保证它们就在那里。

这是包含的代码:

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

我已将 NSLog 语句放在委托方法中,并且我看到它们在使用选取器时在所有其他情况下都被执行。但是,在选择视频的情况下,不会出现 NSLog 语句。

如果有人以前遇到过这个问题并提出解决方案,请分享。找了很多天,也没有找到解决这个问题的办法。

thread 中的一个人遇到了同样的问题,但问题从未解决。

question 作为解决方案必须提供的所有内容,我已在我的程序中实施。

我已按要求添加了以下代码。

委托方法:

- (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.");
    

即使按照建议的时间延迟,picker2 仍然损坏。我使用以下answer 来延迟通话。

这是我第一次尝试选择视频然后选择照片时给出的输出。这是在显示第二个选择器之前先关闭然后延迟运行的。

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

我做了更多的测试,发现选择视频时会调用委托方法,但只有在默认视频播放器显示上单击选择按钮时才会调用。

我仍然不确定如何在从库中选择视频时直接调用委托方法并绕过默认视频播放器。这些额外的信息是否会给任何人其他想法?

【问题讨论】:

我不相信你在说什么。证明给我看。请发布您对didFinishPicking... 的实际实现,包括您证明它“未被调用”的证明。 我将代码添加到问题中。 我正在使用 ios 6.0.1 的 iPhone 3GS 上进行测试。会不会跟这个有关? UIImagePickerController didFinishPickingMediaWithInfo not being called的可能重复 对我来说同样的问题。你解决了吗? 【参考方案1】:

在呈现picker2之前设置属性picker2.allowsEditing=NO

让我知道它是否适合你。

【讨论】:

如果您查看问题中提供的代码,您会发现我已经做到了。它不工作。

以上是关于从库中选择视频时未调用 uiimagepickercontroller didfinishpickingmediawithinfo的主要内容,如果未能解决你的问题,请参考以下文章

iPhone:如何获取从库中选择的视频时长?

如何为从库中选择的视频设置持续时间限制

从库中复制文件时显示 ProgressDialog

从库中提取的视频的 UIImagePickerController 缩略图

iPad 问题:使用 UIImagePicker 从视频文件夹中选择视频

Cordova 相机插件 IOS 11 无法从库中选择图像