我正在编写一个应用程序,用户可以从库中选择照片和视频。我想在选择视频时实现自己的视频播放器,但应用程序会立即启动具有选择按钮的默认视频播放器。
不会调用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.
我做了更多测试,发现在选择视频时调用了委托方法,但只有在默认视频播放器显示屏上点击了选择按钮时才会调用。
我仍然不确定如何在从库中选择视频并绕过默认视频播放器时直接调用委托方法。这些额外信息是否会给其他人提供其他想法?
答案 0 :(得分:-1)
在呈现picker2之前设置属性picker2.allowsEditing=NO
让我知道它是否适合你。