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