2012-05-25 129 views
0

我想從視頻獲取縮略圖(的第一幀),所以我可以顯示它。任何人都可以做到這一點成功?如果是這樣,你做到了嗎?Iphone sdk顯示縮略圖

回答

0

首先你應該有videoURL或nsdata的視頻。現在添加MediaPlayer框架和#import MediaPlayer/MPMoviePlayerController.h。添加這個您要thumbail視頻

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url]; 
UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame]; 
//Player autoplays audio on init 
[player stop]; 
[player release]; 
1

有多種方法第一種是使用AssetImageGenerator號: -

- (UIImage *)imageFromVideoURL 
{ 
// result 
UIImage *image = nil; 

// AVAssetImageGenerator 
AVAsset *asset = [[AVURLAsset alloc] initWithURL:appDelegate.videoURL options:nil];; 
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; 
imageGenerator.appliesPreferredTrackTransform = YES; 

// calc midpoint time of video 
Float64 durationSeconds = CMTimeGetSeconds([asset duration]); 
CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600); 

// get the image from 
NSError *error = nil; 
CMTime actualTime; 
CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:midpoint actualTime:&actualTime error:&error]; 

if (halfWayImage != NULL) 
{ 
    // cgimage to uiimage 
    image = [[UIImage alloc] initWithCGImage:halfWayImage]; 
    [dic setValue:image forKey:kImage]; 
    NSLog(@"Values of dictonary==>%@", dic); 
    NSLog(@"Videos Are:%@",appDelegate.videoURLArray); 
    CGImageRelease(halfWayImage); 
} 
return image; 
} 

這是另一種方法是使用的MPMoviePlayerController的最佳方法: -

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url]; 
UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame]; 
[player stop]; 

這可能會幫助你,謝謝:)

+0

感謝您的回覆.... – user1376474

+0

歡迎是有幫助的嗎? –