0

我正在尝试从视频中获取(第一帧的)缩略图,以便显示它。有人成功地做到了这一点吗?如果是这样,你是怎么做到的?

4

2 回答 2

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];

它可以帮助你谢谢:)

于 2012-05-25T09:55:18.930 回答
0

首先你应该有 videoURL 或 nsdata 的视频。现在添加 MediaPlayer 框架和#import MediaPlayer/MPMoviePlayerController.h。将此添加到您想要视频缩略图的位置

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
//Player autoplays audio on init
[player stop];
[player release];
于 2012-05-25T09:19:20.967 回答