码迷,mamicode.com
首页 > 移动开发 > 详细

ios根据视频地址获取某一帧的图像

时间:2015-05-13 12:16:19      阅读:326      评论:0      收藏:0      [点我收藏+]

标签:

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

ios根据视频地址获取某一帧的图像

标签:

原文地址:http://www.cnblogs.com/huangzs/p/4499742.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!