[英]When UIImagePickerController delegate didFinishPickingMediaWithInfo will be called?
[英]uiimagepickercontroller didfinishpickingmediawithinfo NOT CALLED when selecting a video from the library
我正在編寫一個應用程序,用戶可以從庫中選擇照片和視頻。 我想在選擇視頻時實現自己的視頻播放器,但應用程序會立即啟動具有選擇按鈕的默認視頻播放器。
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語句不會出現。
如果有人之前遇到過此問題並提出解決方案,請分享。 我搜索了很多天,但沒有找到解決這個問題的方法。
這個帖子中的人有同樣的問題,但問題從未解決過。
這個問題必須提供一切解決方案,我已在我的計划中實施。
我按照要求添加了以下代碼。
委托方法:
- (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仍然被打破。 我使用以下答案來延遲通話。
這是我第一次嘗試選擇視頻然后選擇照片時給出的輸出。 在呈現第二個選擇器之前,這是在解除然后延遲的情況下運行的。
> 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.
我做了更多測試,發現在選擇視頻時調用了委托方法,但只有在默認視頻播放器顯示屏上單擊選擇按鈕時才會調用。
我仍然不確定如何在從庫中選擇視頻並繞過默認視頻播放器時直接調用委托方法。 這些額外的信息是否給了其他任何想法?
在呈現picker2之前設置屬性picker2.allowsEditing=NO
如果它適合你,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.