如何在 UIImagePickerController 中显示视频或电影文件?
Posted
技术标签:
【中文标题】如何在 UIImagePickerController 中显示视频或电影文件?【英文标题】:how to show video or movie file into UIImagePickerController? 【发布时间】:2010-06-08 11:36:50 【问题描述】:我正在使用 UIImagePickerController,它使用户能够选择现有照片或使用相机拍摄图像。我可以使用 UIImageView 在我的应用程序中显示该图像。
现在我也想将这种能力用于电影。但我找不到任何方法在我的应用程序中将所选电影显示为图像,就像照片应用程序一样。如您所知,您可以在同一个列表中查看照片和电影。
【问题讨论】:
为什么要将这些答案添加到您的问题中?如果您想包含代码,请将其放入问题中(并将其格式化为代码 - 您现在这样做的方式非常难以理解) 【参考方案1】:-(IBAction) selectImageSelected : (id)sender
actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select Image"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Take Picture", @"Select from gallery", nil];
[actionSheet showInView:self.parentViewController.tabBarController.view];
- (void)actionSheet:(UIActionSheet *)_actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
if(buttonIndex==0)
if( ![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] )
[AlertHandler showAlertMessage:[ErrorMessages getCameraNotFoundMsg] withTitle:@"No Camera Detected"];
return;
else
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imagePicker animated:YES];
else if(buttonIndex==1)
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:imagePicker.sourceType];
[self presentModalViewController:imagePicker animated:YES];
else
[actionSheet dismissWithClickedButtonIndex:2 animated:YES];
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"])
// UIImage *selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSLog(@"found an image");
// [UIImageJPEGRepresentation(image, 1.0f) writeToFile:[self findUniqueSavePath] atomically:YES];
// SETIMAGE(image);
CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);
else if ([mediaType isEqualToString:@"public.movie"])
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSLog(@"found a video");
NSData *webData = [NSData dataWithContentsOfURL:videoURL];
//NSData *video = [[NSString alloc] initWithContentsOfURL:videoURL];
// [webData writeToFile:[self findUniqueMoviePath] atomically:YES];
CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);
CGSize sixzevid=CGSizeMake(imagePicker.view.bounds.size.width,imagePicker.view.bounds.size.height-100);
UIGraphicsBeginImageContext(sixzevid);
[imagePicker.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
eventButton.imageView.image=viewImage;
// NSLog(videoURL);
[picker dismissModalViewControllerAnimated:YES];
/*
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo1
[imagePicker dismissModalViewControllerAnimated:YES];
imagePicker.view.hidden = YES;
eventButton.imageView.image = image;
imageSelected = YES;
*/
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
[imagePicker dismissModalViewControllerAnimated:YES];
imageSelected = NO;
【讨论】:
【参考方案2】:MPMoviePlayerController 的 -thumbnailImageAtTime:timeOption:
方法看起来可以帮助您获得要显示的图像。
【讨论】:
【参考方案3】:只有电影文件的选择器视图
替换
imagePicker.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:imagePicker.sourceType];
与
picker.mediaTypes = [NSArray arrayWithObject:@"public.movie"];
【讨论】:
以上是关于如何在 UIImagePickerController 中显示视频或电影文件?的主要内容,如果未能解决你的问题,请参考以下文章