美文网首页iOS 开发技巧iOS Developer
iOS开发之获取网络mp4视频第一帧

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

作者: Ego_1973 | 来源:发表于2017-07-07 12:21 被阅读47次

写个分类:

 // 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];

相关文章

网友评论

    本文标题:iOS开发之获取网络mp4视频第一帧

    本文链接:https://www.haomeiwen.com/subject/jqmlhxtx.html