获取视频的第一帧

ios取出视频的每一帧图片(关键帧,AVAssetImageGenerator)

方法一:

#import 
#import 
#import 

- (void)movieImage:(UIImage *)image
{
    UIImageView *tempImageView = (UIImageView *)[self viewWithTag:9870];
    [tempImageView setImage:image];
}

- (void)movieToImage
{
  NSURL *url = [NSURL URLWithString:movieURL];
    
  AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:url options:nil];
    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    generator.appliesPreferredTrackTransform=TRUE;
  [asset release];
  CMTime thumbTime = CMTimeMakeWithSeconds(0,30);

    AVAssetImageGeneratorCompletionHandler handler = 
^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
        if (result != AVAssetImageGeneratorSucceeded) {       }//没成功

        UIImage *thumbImg = [[UIImage imageWithCGImage:im] retain];
        [generator release];
        
        [self performSelectorOnMainThread:@selector(movieImage:) withObject:thumbImg waitUntilDone:YES];

    };
    
    generator.maximumSize = self.frame.size;
    [generator generateCGImagesAsynchronouslyForTimes:
[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
    

}

方法二:

 
MPMoviePlayerController 类已经提供了得到帧图片和方法

 
- (UIImage *)thumbnailImageAtTime:(NSTimeInterval)playbackTime timeOption:(MPMovieTimeOption)option NS_DEPRECATED_IOS(3_2, 7_0);

使用:
[movieImage setImage:[tempMovie thumbnailImageAtTime:2.5f timeOption:MPMovieTimeOptionNearestKeyFrame]];

上面是MRC下放在控制器中的:
下面的是ARC下放在控件中的:

#import "ViewController.h"
#import 
#import 
#import 

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self movieToImage];

    
    
}

- (void)movieImage:(UIImage *)image
{

    self.imageView.image = image;

    
}


- (void)movieToImage
{
    NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/a45016f4-08d6-4277-abe6-bcfd5244c201/L.mp4"];
    AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:url options:nil];
    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    generator.appliesPreferredTrackTransform=TRUE;
    
    CMTime thumbTime = CMTimeMakeWithSeconds(0,30);
    
    AVAssetImageGeneratorCompletionHandler handler =
    ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){

        if (result != AVAssetImageGeneratorSucceeded) {
        }//没成功
        
        UIImage *thumbImg = [UIImage imageWithCGImage:im];

            [self performSelectorOnMainThread:@selector(movieImage:) withObject:thumbImg waitUntilDone:YES];
        };
        generator.maximumSize = self.view.frame.size;
        [generator generateCGImagesAsynchronouslyForTimes:
         [NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

你可能感兴趣的:(获取视频的第一帧)