应用程序崩溃来自uiimagepickercontroller的选择视频文件

时间:2012-06-15 06:33:39

标签: iphone objective-c ios4 uiimagepickercontroller

在我的应用程序中,我想使用UIImagePickerController从库中选择视频。但是当我选择持续时间超过5分钟的视频时,我的应用程序在ipod上崩溃了。如何限制用户选择持续时间超过5分钟的视频。请给我这个bug的解决方案。谢谢。

3 个答案:

答案 0 :(得分:1)

试试这个:  

NSURL * aUrlMovie = [[info valueForKey:UIImagePickerControllerMediaURL] retain];/
 AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:aURLPath];
CMTime duration = playerItem.duration;
float seconds = CMTimeGetSeconds(duration);

然后为您希望的秒数添加验证..

答案 1 :(得分:1)

 #pragma mark UIImagePickerController delegate

  - (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
  {
NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ( [mediaType isEqualToString:(NSString*)kUTTypeMovie] ) {
    self.mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
  if ( UIVideoAtPathIsCompatibleWithSavedPhotosAlbum( mediaPath ) ) {
        UISaveVideoAtPathToSavedPhotosAlbum( mediaPath,
                                            self,
                                            @selector(video:didFinishSavingWithError:contextInfo:),
                                            NULL );
    } 
} 
[self dismissModalViewControllerAnimated:YES];
}

答案 2 :(得分:1)

尝试 picker.videoMaximumDuration = 5;

这样的事情:

 #import <AVFoundation/AVFoundation.h>
 #import <AVFoundation/AVAsset.h>

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
   NSURL *selectedVideoUrl = [info objectForKey:UIImagePickerControllerMediaURL];

   AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:selectedVideoUrl];

   CMTime duration = playerItem.duration;
  float seconds = CMTimeGetSeconds(duration);
  //NSLog(@"duration: %.2f", seconds);
  if(seconds > 300)
  {
     [picker popViewControllerAnimated:YES];
  }
  else{
  [picker dismissModalViewControllerAnimated:YES];
  }

}