MovieTableViewTest

//

//  ViewController.m

//  MovieTableViewTest

//

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

//  Copyright © 2015 lin. All rights reserved.

//


#import "ViewController.h"

#import "TableViewCell.h"

#import <AVFoundation/AVFoundation.h>

#import <MediaPlayer/MediaPlayer.h>

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

{

    UITableView *_tableView;

    NSArray *_arrayName;

    UIImage *_image;

    UIButton *_backButton;

    NSMutableArray *arrayPic;

    UIImageView *imagePic;

    int i;

}

@property (nonatomic,strong)MPMoviePlayerController *moviePlayer;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    [self layoutUI];

    imagePic = [[UIImageView alloc]init];

    arrayPic = [NSMutableArray array];

    i = 0;

    for (int pic = 0; pic < _arrayName.count; pic++) {

         [self imageRequest : 50.0 localPic:pic];

    }

}


- (void)imageRequest : (CGFloat)timeBySecond localPic:(int)num{

    //创建url

    

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:_arrayName[num] 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

    _image = [UIImage imageWithCGImage:cgImage];

    

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

    imageView.contentMode = UIViewContentModeScaleAspectFit;

    imageView.image = _image;

    

    [arrayPic addObject:imageView];

//    [self.view addSubview:imageView];

}


- (void)layoutUI {

    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, 375, 647)];

    _tableView.dataSource = self;

    _tableView.delegate = self;

    

    [self.view addSubview:_tableView];

    

    _arrayName = @[@"煎饼侠",@"心在跳"];

    

    

}


- (void)back {

    [_moviePlayer.view removeFromSuperview];

    _moviePlayer = nil;

}


#pragma mark 设置分区高度

//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

//   

//}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 120;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _arrayName.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    TableViewCell * Cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (Cell == nil) {

        Cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

    }

//    Cell.backgroundColor = [UIColor grayColor];

    Cell.label.text = _arrayName[indexPath.row];

    imagePic = arrayPic[indexPath.row];

    Cell.image.image = imagePic.image;

    return Cell;

}


#pragma mark cell点击事件


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    i=(int)indexPath.row;

    [self.moviePlayer play];

}


#pragma mark 创建视频播放控制器

- (MPMoviePlayerController *)moviePlayer{

    

    if (!_moviePlayer) {

        //1.获取视频地址(可以本地,也可以网络)

        NSString *urlStr =  [[NSBundle mainBundle]pathForResource:_arrayName[i] ofType:@"mp4"];

        NSURL *url = [NSURL fileURLWithPath:urlStr];

        //2.初始化播放控制器

        _moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];

        _moviePlayer.view.frame = self.view.frame;

        

        //播放器视图->自适应屏幕宽高

        _moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

        [self.view addSubview:_moviePlayer.view];

        _backButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 40, 20)];

        [_backButton setTitle:@"x" forState:UIControlStateNormal];

        _backButton.backgroundColor = [UIColor redColor];

        [_backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

        [_moviePlayer.view addSubview:_backButton];

    }

    return _moviePlayer;

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


重写

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

//

//  TableViewCell.m

//  MovieTableViewTest

//

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

//  Copyright © 2015 lin. All rights reserved.

//


#import "TableViewCell.h"


@implementation TableViewCell


-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    NSArray *array = [[NSBundle mainBundle]loadNibNamed:@"tableViewCell" owner:self options:nil];

    self = [array firstObject];

    return self;

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];


    // Configure the view for the selected state

}


@end


转载于:https://my.oschina.net/u/2499773/blog/592709

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值