关闭

获取视频的缩略图

332人阅读 评论(0) 收藏 举报
分类:

【1】 获取本地视频的缩略图

+(UIImage *)getThumbnailImage:(NSString *)videoURL


{

    

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:videoURL] options:nil];

    

    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];

    

    gen.appliesPreferredTrackTransform = YES;

    

    CMTime time = CMTimeMakeWithSeconds(0.0, 600);

    

    NSError *error = nil;

    

    CMTime actualTime;

    

    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];

    

    UIImage *thumb = [[UIImage alloc] initWithCGImage:image];

    

    CGImageRelease(image);

    

    return thumb;

}


第二种方法

需要添加AVFoundation和CoreMedia.framework

另外一种那个方法

MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:videoURL]; moviePlayer.shouldAutoplay = NO; 
UIImage *thumbnail = [moviePlayer thumbnailImageAtTime:time timeOption:MPMovieTimeOptionNearestKeyFrame];
//这个也一样

+(UIImage *)fFirstVideoFrame:(NSString *)path 

    MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] 
                                   initWithContentURL:[NSURL fileURLWithPath:path]]; 
    UIImage *img = [mp thumbnailImageAtTime:0.0 
                                 timeOption:MPMovieTimeOptionNearestKeyFrame]; 
    [mp stop]; 
    [mp release]; 
    return img; 
}


【2】 获取网络视频的缩略图


+ (UIImage *)getImageWihtUrl:(NSString *)videoURL

{

    NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];

    //    NSURL *url = [[[NSURL alloc] initFileURLWithPath:videoURL] autorelease];

    NSURL *url=[[NSURL alloc]initWithString:videoURL];

    AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:url options:opts];

    AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset];

    generator.appliesPreferredTrackTransform = YES;

    generator.maximumSize = CGSizeMake(240, 150);

    NSError *error = nil;

    CGImageRef img = [generator copyCGImageAtTime:CMTimeMake(60, 10) actualTime:NULL error:&error];

    UIImage *image = [UIImage imageWithCGImage: img];

    return image;

}




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:39358次
    • 积分:1890
    • 等级:
    • 排名:千里之外
    • 原创:153篇
    • 转载:13篇
    • 译文:1篇
    • 评论:3条
    文章分类
    最新评论