从库中选择视频时,uiimagepickercontroller didfinishpickingmediawithinfo未调用 [英] uiimagepickercontroller didfinishpickingmediawithinfo NOT CALLED when selecting a video from the library

查看:98
本文介绍了从库中选择视频时,uiimagepickercontroller didfinishpickingmediawithinfo未调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

didfinishpickingmediawithinfo函数未调用.仅在选择视频时才会发生这种情况.我可以在屏幕上显示选定的照片,因为选择照片时会调用委托方法.

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

单击库按钮的代码:

 //图书馆访问功能可显示存储在设备上的照片和视频-(IBAction)selectPhoto:(UIButton *)sender {UIImagePickerController * picker2 = [[UIImagePickerController alloc] init];picker2.delegate =自我;picker2.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;picker2.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker2.sourceType];picker2.allowsEditing = NO;self.picker2 = picker2;[self.picker dismissViewControllerAnimated:YES完成:^ {[self presentViewController:self.picker2动画:是完成:NULL];}];} 

我确实将委托包含在视图控制器中.我研究过的许多问题指出,视图控制器的头文件中缺少包含内容,但我向您保证它们在那里.

以下是包含代码:

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

我已经将NSLog语句放置在委托方法中,并且看到在使用选择器的所有其他情况下它们都将执行.但是,在选择视频的情况下,不会显示NSLog语句.

如果任何人以前曾遇到此问题并提出解决方案,请分享.我已经搜索了很多天,却没有找到解决该问题的方法.

线程中的人遇到相同的问题,但问题从未解决.

>问题所提供的一切都是解决方案,我已经实现了程序.

我已根据要求添加了以下代码.

委托方法:

 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {NSLog(@"didfinishpicking方法已触发");//获取所选的媒体类型NSString * mediaType = [info objectForKey:UIImagePickerControllerMediaType];//处理媒体捕获(图像/视频)//拍摄图像时如果([mediaType isEqualToString:(NSString *)kUTTypeImage]&& picker.sourceType == UIImagePickerControllerSourceTypeCamera){//将拍摄的照片保存到相机胶卷库UIImage * imageTaken = [信息valueForKey:UIImagePickerControllerOriginalImage];UIImageWriteToSavedPhotosAlbum(imageTaken,nil,nil,nil);//更新库按钮图像[self.imageButton setImage:imageTaken forState:UIControlStateNormal];}//拍摄视频时否则([mediaType isEqualToString:(NSString *)kUTTypeMovie]&& picker.sourceType == UIImagePickerControllerSourceTypeCamera){//抓取刚刚拍摄的视频的URLNSURL * newVideo = [info objectForKey:UIImagePickerControllerMediaURL];//将视频保存到相机胶卷UISaveVideoAtPathToSavedPhotosAlbum([newVideo path],nil,nil,nil);}//处理库预览//选择图片时否则((mediatype isEqualToString:(NSString *)kUTTypeImage]& picker.sourceType!= UIImagePickerControllerSourceTypeCamera){NSLog(@从库中选择了一张图片.");UIImage * image = [info valueForKey:UIImagePickerControllerOriginalImage];self.libraryImage.image =图片;self.libraryView.translatesAutoresizingMaskIntoConstraints = YES;[[UIApplication sharedApplication] .keyWindow addSubview:self.libraryView];}//选择视频时否则([mediaType isEqualToString:(NSString *)kUTTypeMovie]&& picker.sourceType!= UIImagePickerControllerSourceTypeCamera){NSLog(@从库中选择了一个视频.");}} 

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

这是我第一次尝试选择视频然后选择照片时给出的输出.在提交第二个选择器之前,先进行了解雇,然后进行了一段时间的拖延.

 >2014-12-30 00:11:57.763偷拍[228:907] [MPAVController]自动播放:>启用自动播放功能2014-12-30 00:11:57.766潜行[228:907]>[MPAVController]自动播放:跳过自动播放,已禁用(针对当前>项目:0,播放器:1)2014-12-30 00:11:57.853潜行[228:907]>[MPAVController]自动播放:启用自动播放2014-12-30 00:11:58.052>潜行[228:907] [MPCloudAssetDownloadController]优先级>要求提供媒体项目ID:0 2014-12-30 00:11:58.897潜行[228:907]>[MPAVController]自动播放:跳过自动播放,已禁用(针对当前>项目:0,播放器:1)2014-12-30 00:11:58.911潜行[228:907]>[MPAVController]自动播放:_streamLikelyToKeepUp:0->1 2014-12-30>00:12:17.576潜行[228:907] didfinishpicking方法已触发>2014-12-30 00:12:17.579偷拍[228:907]从>图书馆. 

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

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

解决方案

在显示picker2之前,请设置属性 picker2.allowsEditing = NO

让我知道它是否对您有用.

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.

这篇关于从库中选择视频时,uiimagepickercontroller didfinishpickingmediawithinfo未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆