iOS获取本地视频和网络URL视频的缩略图方法

首先大家先添加AVFoundation和CoreMedia.framework两个框架

NSString *path = @"www.51ios.net/本地路径"
MPMoviePlayerController *51iosMPMovie = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:path]];
UIImage *img = [51iosMPMovie thumbnailImageAtTime:0.0
timeOption:MPMovieTimeOptionNearestKeyFrame];

此处的img就是时间在0.0的缩略图

 

  • 第二种获取网络视频的缩略图

NSString *videoURL = @"http://www.51ios.net/archives/784"

MPMoviePlayerController *51iosMPMovie = [[MPMoviePlayerController alloc]initWithContentURL:videoURL]; 51iosMPMovie.shouldAutoplay = NO;
UIImage *thumbnail = [51iosMPMovie thumbnailImageAtTime:time timeOption:MPMovieTimeOptionNearestKeyFrame];

此处的thumbnail就是网络视频的缩略图

 

  • 第三站方法用AVFoundation实现

+(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;
}

 



文/Lonely__(简书作者)
原文链接:http://www.jianshu.com/p/cc52403f7d42
 

根据url获取视频的缩略图和数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dengmeiyu/article/details/46311567

获取缩略图方法一:

导入

#import <AVFoundation/AVFoundation.h>

#import <MediaPlayer/MediaPlayer.h>

 

                MPMoviePlayerController *player = [[MPMoviePlayerControlleralloc]initWithContentURL:url];

                UIImage  *thumbnail = [playerthumbnailImageAtTime:1.0timeOption:MPMovieTimeOptionNearestKeyFrame];

                [playerstop];

获取缩略图方法二:
#import <AVFoundation/AVFoundation.h>

#import <AssetsLibrary/ALAssetsLibrary.h>

+ (UIImage *)getVideoThumbnailImage:(NSURL *)videoURL {

   AVURLAsset *asset = [[AVURLAssetalloc]initWithURL:videoURLoptions:nil];

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

    gen.appliesPreferredTrackTransform =YES;

    CMTime time =CMTimeMakeWithSeconds(0.0,600);

   NSError *error =nil;

   CMTime actualTime;

   CGImageRef image = [gencopyCGImageAtTime:time actualTime:&actualTimeerror:&error];

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

    CGImageRelease(image);

   return thumb;

}

获取数据流:

 

 NSData *data = [NSDatadataWithContentsOfURL:videoURL];

posted @ 2018-05-26 00:21  sundaysios  阅读(493)  评论(0)    收藏  举报