2009-10-05 11 views
12

Próbuję uzyskać pierwszą klatkę z wybranego wideo w UIImagePickerController, aby pokazać ją w UIImageView, ale nie wiem, czy to możliwe. Jeśli tak, to jak mam to zrobić?Chwytanie pierwszej klatki wideo z UIImagePickerController?

+0

podobne pytanie http://stackoverflow.com/questions/1347562/getting-thumbnail-from-a-video-url-or-data-in-iphone-sdk –

Odpowiedz

27

Możesz to zrobić na jeden z dwóch sposobów. Pierwszym sposobem jest użycie MPMoviePlayerController chwycić miniatury

MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] 
             initWithContentURL:videoURL]; 
moviePlayer.shouldAutoplay = NO; 
UIImage *thumbnail = [moviePlayer thumbnailImageAtTime:time 
        timeOption:MPMovieTimeOptionNearestKeyFrame]; 

To działa, ale MPMoviePlayerController nie jest szczególnie lekki przedmiot i nie jest szczególnie szybko chwytając miniatury.

Preferowanym sposobem jest użycie nowego parametru AVAssetImageGenerator w AVFoundation. Jest szybki, lekki i bardziej elastyczny niż stary sposób. Oto metoda pomocnicza, która zwróci autoodtwarzany obraz z filmu.


+ (UIImage *)thumbnailImageForVideo:(NSURL *)videoURL 
          atTime:(NSTimeInterval)time 
{ 

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil]; 
    NSParameterAssert(asset); 
    AVAssetImageGenerator *assetIG = 
       [[AVAssetImageGenerator alloc] initWithAsset:asset]; 
    assetIG.appliesPreferredTrackTransform = YES; 
    assetIG.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels; 

    CGImageRef thumbnailImageRef = NULL; 
    CFTimeInterval thumbnailImageTime = time; 
    NSError *igError = nil; 
    thumbnailImageRef = 
      [assetIG copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60) 
          actualTime:NULL 
            error:&igError]; 

    if (!thumbnailImageRef) 
     NSLog(@"thumbnailImageGenerationError %@", igError); 

    UIImage *thumbnailImage = thumbnailImageRef 
          ? [[UIImage alloc] initWithCGImage:thumbnailImageRef] 
          : nil; 

    return thumbnailImage; 
} 

użycie Asynchronous


- (void)thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time completion:(void (^)(UIImage *)) completion 
{ 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ 

     AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil]; 
     NSParameterAssert(asset); 
     AVAssetImageGenerator *assetIG = 
     [[AVAssetImageGenerator alloc] initWithAsset:asset]; 
     assetIG.appliesPreferredTrackTransform = YES; 
     assetIG.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels; 

     CGImageRef thumbnailImageRef = NULL; 
     CFTimeInterval thumbnailImageTime = time; 
     NSError *igError = nil; 
     thumbnailImageRef = 
     [assetIG copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60) 
         actualTime:NULL 
          error:&igError]; 

     if (!thumbnailImageRef) 
      NSLog(@"thumbnailImageGenerationError %@", igError); 

     UIImage *thumbnailImage = thumbnailImageRef 
     ? [[UIImage alloc] initWithCGImage:thumbnailImageRef] 
     : nil; 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      completion(thumbnailImage); 
     }); 
    }); 
} 
+1

Musisz dodać ramy "Core Media" również do swojego celu. –

+0

+1 Mój kolega nie był w stanie napisać czegoś takiego, ale do ciebie ... –

+0

dla innych, którzy próbują tego użyć: zwróć uwagę, jeśli wideoURL zostało utworzone przez fileURLWithPath lub URLWithString (gdy masz ścieżkę w NSString *) – user1105951

Powiązane problemy