如何在 iOS 中使用 UIImagePickerController 从 photoGallery 获取视频?

How to get video from photoGallery using UIImagePickerController in iOS?

我想在单击视频按钮并显示在同一视图中时从 photoGallery 获取视频。我已经编写了这段代码,但不要在同一视图中将视频放入图像视图中。

- (IBAction)vedioClicked:(id)sender
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;

imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes =
[[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
[self presentViewController:imagePicker animated:YES completion:nil];

}

- (void) imagePickerController: (UIImagePickerController *) picker  didFinishPickingMediaWithInfo: (NSDictionary *) info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) 

{
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
inputImage = UIGraphicsGetImageFromCurrentImageContext();
self.imageView.image=inputImage;

// End the context

UIGraphicsEndImageContext();

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

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

NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

}
self.imageView.image=inputImage;   
[picker dismissViewControllerAnimated:YES completion:nil];

}

视频是从照片库中获取的,但它不会在同一视图中显示到 imageView 中,所以任何人帮助我做 this.Any 帮助都非常感谢。

我们不能直接在UIImageView中显示视频。如果我们尝试直接加载它只会显示黑屏。要在 UIImageView 中显示视频,我们需要加载视频的第一帧。为此需要导入两个框架。

1.AVFoundation

2.AssetsLibrary

使用这两个我们可以将视频的第一帧显示到 UIImageView 中,如下所示:

- (void) imagePickerController: (UIImagePickerController *) picker  didFinishPickingMediaWithInfo: (NSDictionary *) info
{
   NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

  if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) 
  {
     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
   inputImage = UIGraphicsGetImageFromCurrentImageContext();
   self.imageView.image=inputImage;

   // End the context

    UIGraphicsEndImageContext();

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

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

    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

    AVAsset *avAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil];

    if ([[avAsset tracksWithMediaType:AVMediaTypeVideo] count] > 0)
    {
      AVAssetImageGenerator *imageGenerator =[AVAssetImageGenerator assetImageGeneratorWithAsset:avAsset];
      Float64 durationSeconds = CMTimeGetSeconds([avAsset duration]);
      CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600);
      NSError *error;
      CMTime actualTime;

      CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:kCMTimeZero actualTime:&actualTime error:&error];

      if (halfWayImage != NULL)
      {

         NSString *actualTimeString = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, actualTime));
         NSString *requestedTimeString = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, midpoint));
         NSLog(@"Got halfWayImage: Asked for %@, got %@", requestedTimeString, actualTimeString);

          UIImage *img=[UIImage imageWithCGImage:halfWayImage];
          self.imageView.image=img;

       }

     }
  }

   [picker dismissViewControllerAnimated:YES completion:nil];

}