iOS开发之获取网络mp4视频第一帧

写个分类:

 // UIImage+WBFirstImage.h
 + (UIImage*) thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time;


  // UIImage+WBFirstImage.m
 #import <AVFoundation/AVFoundation.h>
 #import <CoreMedia/CoreMedia.h>
 #import <Foundation/NSObject.h>
 #import <Foundation/NSNotification.h>

 + (UIImage*) thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time {

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
NSParameterAssert(asset);
AVAssetImageGenerator *assetImageGenerator =[[AVAssetImageGenerator alloc] initWithAsset:asset];
assetImageGenerator.appliesPreferredTrackTransform = YES;
assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;

CGImageRef thumbnailImageRef = NULL;
CFTimeInterval thumbnailImageTime = time;
NSError *thumbnailImageGenerationError = nil;
thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 10)actualTime:NULL error:&thumbnailImageGenerationError];

if(!thumbnailImageRef)
    NSLog(@"thumbnailImageGenerationError %@",thumbnailImageGenerationError);

UIImage*thumbnailImage = thumbnailImageRef ? [[UIImage alloc]initWithCGImage: thumbnailImageRef] : nil;

return thumbnailImage;
}
调用
 UIImage *firstImage  = [UIImage thumbnailImageForVideo:[NSURL URLWithString:_statusModel.videoUrl] atTime:10];
如果我们想在这张图片上加个占位图却不想再创建一个imageView呢?
 - (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {
UIGraphicsBeginImageContext(image1.size);

UIGraphicsBeginImageContext(image2.size);

[image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];

[image1 drawInRect:CGRectMake(image2.size.width/2-70, image2.size.height/2-70, 140, 140)];

UIImage *resultImage=UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return resultImage;
  }
调用
 UIImage *addImage = [self addImage:[UIImage imageNamed:@"play"] toImage:firstImage];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,118评论 25 709
  • 原文地址 http://www.cnblogs.com/kenshincui/p/3983982.html 大家都...
    怎样m阅读 1,321评论 0 1
  • 前不久,一位来自龙华的朋友专程赶到公司和我一起探讨他正在运作的家庭用农产品快消品项目,运作方式也是基于社区O2O的...
    价值分享阅读 514评论 0 1
  • 旅行,听起来对于我这个还挣扎在温饱线,而且没有什么特殊技能的人来说,是何其奢侈的。大学毕业到现在,近两年的...
    凯旋的冰淇淋阅读 221评论 0 1
  • 一、对象(object) JavaScript 中的所有事物都是对象:字符串、数值、数组、函数... 使用 Str...
    AnnQi阅读 186评论 0 0