【问题标题】:Pick a video from iPhone photo library从 iPhone 照片库中选择一个视频
【发布时间】:2014-04-23 01:58:33
【问题描述】:

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

设置电影路径:(null)

设置影片路径:/Users/Mahesh/Library/Application Support/iPhone 模拟器/6.1/Media/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];

}

请帮帮我.. 谢谢!

【问题讨论】:

  • 我在 iOS 6.1 上遇到了同样的问题,但代码在 iOS7 上仍然运行良好。
  • 您使用的是仿真器还是实际设备?我认为您的代码可以在设备上运行,但会在模拟器上崩溃。

标签: ios iphone objective-c xcode5 uiimagepickercontroller


【解决方案1】:

试试

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

【讨论】:

    【解决方案2】:

    一切看起来都不错。

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

    我只能建议两件事:

    1. 尝试将 imagePicker 声明为属性。

    2.当你检查是图片还是视频时,替换

    [mediaType isEqualToString:@"public.image"]
    

    与

    [mediaType isEqualToString:(NSString *)kUTTypeImage]
    

    并留下视频以供 else 声明。

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

    编辑:也可以尝试添加imagePicker.allowsEditing = NO;

    【讨论】:

    • 我已经在头文件中设置了委托。我也导入了 ,但问题仍然存在:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多