首页 > 代码库 > iOS获取视频中的指定帧的两种方法
iOS获取视频中的指定帧的两种方法
方法一 :AVFoundation
1 #import <AVFoundation/AVFoundation.h> 2 3 - (UIImage *)thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time { 4 AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil]; 5 NSParameterAssert(asset); 6 AVAssetImageGenerator *assetImageGenerator =[[AVAssetImageGenerator alloc] initWithAsset:asset]; 7 assetImageGenerator.appliesPreferredTrackTransform = YES; 8 assetImageGenerator.apertureMode =AVAssetImageGeneratorApertureModeEncodedPixels; 9 10 CGImageRef thumbnailImageRef = NULL; 11 CFTimeInterval thumbnailImageTime = time; 12 NSError *thumbnailImageGenerationError = nil; 13 thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)actualTime:NULL error:&thumbnailImageGenerationError]; 14 15 if(!thumbnailImageRef) 16 CXIMLog(@"thumbnailImageGenerationError %@",thumbnailImageGenerationError); 17 18 UIImage *thumbnailImage = thumbnailImageRef ? [[UIImage alloc]initWithCGImage:thumbnailImageRef] : nil; 19 return thumbnailImage; 20 }
方法二:MPMoviePlayerController
1 #import <MediaPlayer/MediaPlayer.h> 2 3 MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL]; 4 moviePlayer.shouldAutoplay = NO; 5 UIImage *thumbnailImage = [moviePlayer thumbnailImageAtTime:time timeOption:MPMovieTimeOptionNearestKeyFrame];
iOS获取视频中的指定帧的两种方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。