从iPhone照片库中选择一个视频

时间:2014-03-17 17:45:54

标签: ios iphone objective-c xcode5 uiimagepickercontroller

我正在尝试使用UIImagePickerController从iPhone照片库中选择一个视频,以便我可以将其上传到服务器。 我能够使用UIImagePickerController选择图像而没有任何错误/崩溃,但在选择时(不是实际点击选择按钮,而只是从库中选择视频)视频我的应用程序崩溃。 它只是在控制台上打印以下行:

  

设置电影路径:( null)

     

设置电影路径:/ Users / Mahesh / Library / Application Support / iPhone   模拟器/ 6.1 /媒体/ DCIM / 100APPLE / IMG_0006.mp4

在调用我的应用程序崩溃之前,委托方法 didFinishPickingMediaWithInfo 没有被调用。我只是无法弄清楚究竟是什么导致了这个问题。

以下是我打开iPhone库的代码:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
//imagePicker.mediaTypes = [NSArray arrayWithObjects:@"public.movie", nil];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:imagePicker.sourceType];

[self presentViewController:imagePicker animated:YES completion:nil];


- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info
{

NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"]){
    UIImage *editedImage = (UIImage *)[info objectForKey:@"UIImagePickerControllerOriginalImage"];

    // Compressing the image size
    CGSize newSize = CGSizeMake(400, 400);
    UIGraphicsBeginImageContext(newSize);

    [editedImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    // End the context
    UIGraphicsEndImageContext();

    NSData *data =  UIImageJPEGRepresentation(newImage,0.2);//PNGRepresentation(imgView.image);

}
else if ([mediaType isEqualToString:@"public.movie"]){

    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
}

[self dismissViewControllerAnimated:YES completion:nil];

}

请帮帮我..谢谢!

2 个答案:

答案 0 :(得分:1)

尝试

picker.mediaTypes = @[(NSString *)kUTTypeMovie, (NSString *)kUTTypeImage];

答案 1 :(得分:0)

一切看起来都不错。

您是否在头文件中设置了委托?

我只能建议两件事:

  1. 尝试将imagePicker声明为属性。
  2. 2.当您检查它的图像或视频时,请替换

    [mediaType isEqualToString:@"public.image"]
    

    [mediaType isEqualToString:(NSString *)kUTTypeImage]
    

    并将视频留给else语句。

    您需要导入<MobileCoreServices/UTCoreTypes.h>才能进行此项检查。 希望这会有所帮助。

    修改:您还可以尝试添加imagePicker.allowsEditing = NO;