PublicUtils.m
+(UIImage *) imageWithVideoUrl:(NSURL *)videoURL time:(CMTime) time
{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
[asset release];
asset = nil;
gen.appliesPreferredTrackTransform = YES;
NSError *error =nil;
CMTime actualTime;
//获取time处的视频截图
CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
[gen release];
gen = nil;
//将CGImageref转换成UIImage
UIImage *thumb = [[[UIImage alloc] initWithCGImage:image] autorelease];
CGImageRelease(image);
return thumb;
}
例如获取第一帧截图:
NSURL* Url = [NSURL URLWithString:urlStr];
CMTime time = CMTimeMake(0,30);
[PublicUtils imageWithVideoUrl:Url time:time]
关于CMTime
CMTimeMake(a,b) a当前第几帧, b每秒钟多少帧.当前播放时间a/b
CMTimeMakeWithSeconds(a,b) a当前时间,b每秒钟多少帧.
CMTimeMake
CMTime CMTimeMake (
int64_t value,
int32_t timescale
);