如何从iOS 9和Objective C中的.MOV文件中获取缩略图?

时间:2016-07-24 17:51:12

标签: objective-c ios9 uiimagepickercontroller phasset

选择或拍摄视频后,我想用下面的同伴代表旁边的视频更新我的UIUImageView。对于iOS 9和PHAsset,如何使用Objective C执行此操作?

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
  NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
  if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
    NSURL *videoUrl=(NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];
    NSData *videoData = [NSData dataWithContentsOfURL:videoUrl];
  }
}

3 个答案:

答案 0 :(得分:3)

尝试使用以下代码执行此操作,其中asset参数是您的PHAsset实例:

PHImageManager *manager = [PHImageManager defaultManager];

[manager requestImageForAsset: asset
                   targetSize: CGSizeMake(100.0, 100.0) // use required size
                  contentMode: PHImageContentModeAspectFill
                      options: nil
                resultHandler: ^(UIImage * _Nullable anImage, NSDictionary * _Nullable info) {

                    dispatch_async(dispatch_get_main_queue(), ^{
                        yourImageView.image = anImage;
                    });
 }];

答案 1 :(得分:2)

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

  self.sourceURLString =[NSString stringWithFormat:@"%@", [info                                                         objectForKey:UIImagePickerControllerMediaURL]];

  dispatch_async(dispatch_get_main_queue(), ^{
    self.thumbImageView.image = [self generateThumbImage:self.sourceURLString];
    self.addVideoButton.hidden = YES;
   });

}

-(UIImage *)generateThumbImage : (NSString *)sourceURLString{
  NSURL *sourceMovieURL = [NSURL URLWithString:sourceURLString];
  AVURLAsset *sourceAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];
  AVAssetImageGenerator *generateImg = [[AVAssetImageGenerator alloc] initWithAsset:sourceAsset];
  NSError *error = NULL;
  CMTime time = CMTimeMake(1, 65);
  CGImageRef refImg = [generateImg copyCGImageAtTime:time actualTime:NULL error:&error];
  NSLog(@"error==%@, Refimage==%@", error, refImg);

  return [[UIImage alloc] initWithCGImage:refImg];
}

答案 2 :(得分:0)

 + (UIImage *)imageFromMovie:(NSURL *)movieURL atTime:(NSTimeInterval)time {
  // set up the movie player
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] 
initWithContentURL:movieURL];
mp.shouldAutoplay = NO;
mp.initialPlaybackTime = time;
mp.currentPlaybackTime = time;
// get the thumbnail
UIImage *thumbnail = [mp thumbnailImageAtTime:time 
                       timeOption:MPMovieTimeOptionNearestKeyFrame];
// clean up the movie player
[mp stop];
[mp release];
return(thumbnail);

}