擴展-使用AVFoundation生成縮略圖

//url

//  ViewController.mspa

//  擴展-使用AVFoundation生成縮略圖.net

//3d

//  Created by dc008 on 15/12/29.orm

//  Copyright © 2015 lin. All rights reserved.視頻

//對象


#import "ViewController.h"圖片

#import <AVFoundation/AVFoundation.h>get

@interface ViewController ()it


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    [self imageRequest:50.0];

}


- (void)imageRequest : (CGFloat)timeBySecond{

    //建立url

    

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"0" ofType:@"mp4"]];

    //根據url建立AVURLAsset

    AVURLAsset *urlAsset = [AVURLAsset assetWithURL:url];

    //根據AVURLAsset建立 AVAssetImageGenerator(圖片生成對象)

    AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset];

    //建立截取的時間點(參數:視頻第幾秒,每秒幀數)

    CMTime time = CMTimeMakeWithSeconds(timeBySecond, 10);

    //接收實際截取圖片時間

    CMTime actualTime;

    CGImageRef cgImage = [imageGenerator copyCGImageAtTime:time actualTime:&actualTime error:nil];

    //顯示實際時間

    CMTimeShow(actualTime);

    //CGImageRef -> UIImage

    UIImage *image = [UIImage imageWithCGImage:cgImage];

    

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375, 375)];

    imageView.contentMode = UIViewContentModeScaleAspectFit;

    imageView.image = image;

    [self.view addSubview:imageView];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

相關文章
相關標籤/搜索