首页 > 代码库 > 根据视频地址获取某一帧的图像
根据视频地址获取某一帧的图像
http://blog.fuckbugs.me/category/ios/
//CatchImage.h
#import <Foundation/Foundation.h>
@interface CatchImage : NSObject
/*
*videoURL:视频地址(本地/网络)
*time :第N帧
*/
+ (UIImage*) thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time;
@end
//CatchImage.m
#import "CatchImage.h"
#import <AVFoundation/AVFoundation.h>
#import <AVFoundation/AVAssetImageGenerator.h>
#import <AVFoundation/AVAsset.h>
@implementation CatchImage
+ (UIImage*) thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time {
AVURLAsset *asset = [[[AVURLAsset alloc] initWithURL:videoURL options:nil]autorelease];
NSParameterAssert(asset);
AVAssetImageGenerator *assetImageGenerator =[[[AVAssetImageGenerator alloc] initWithAsset:asset]autorelease];
assetImageGenerator.appliesPreferredTrackTransform = YES;
assetImageGenerator.apertureMode =AVAssetImageGeneratorApertureModeEncodedPixels;
CGImageRef thumbnailImageRef = NULL;
CFTimeInterval thumbnailImageTime = time;
NSError *thumbnailImageGenerationError = nil;
thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)actualTime:NULL error:&thumbnailImageGenerationError];
if(!thumbnailImageRef)
NSLog(@"thumbnailImageGenerationError %@",thumbnailImageGenerationError);
UIImage*thumbnailImage = thumbnailImageRef ? [[[UIImage alloc]initWithCGImage:thumbnailImageRef] autorelease] : nil;
return thumbnailImage;
}
@end
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。