获取视频的缩略图

需要加入

#import <MediaPlayer/MediaPlayer.h>


主要使用

//获取视频的thumbnail

            MPMoviePlayerController *player = [[MPMoviePlayerController alloc]initWithContentURL:videoUrl];

            player.shouldAutoplay = NO;

            UIImage  *thumbnail = [player thumbnailImageAtTime:0.0 timeOption:MPMovieTimeOptionNearestKeyFrame];

            player = nil;


其中的videoUrl通过下面的协议获取。。。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

NSURL* videoUrl=[info objectForKey:@"UIImagePickerControllerMediaURL"];

}

以上的方法好像只能在主线程里用。

还有一种方法也能获取视频的缩略图。

-(void)getThumbnail1:(NSString*)videoUrl

{

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString: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];//thumb就是获取到的视频的缩略图

    NSString* fileName=[[videoUrl lastPathComponent] stringByReplacingOccurrencesOfString:@"mp4" withString:@"jpg"];

    NSString* imgPath=[[ChinaNewsInterface documentFolder]stringByAppendingPathComponent:fileName];

    NSData* data=UIImageJPEGRepresentation(thumb, 0.3);

    [data writeToFile:imgPath atomically:YES];

    CGImageRelease(image);

}

其中,参数videoUrl是视频的url,注意是NSString,不是NSUrl。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值