ViewController.m文件數組
———————————————————————————————————————————————————————————————app
#import "ViewController.h"ide
#import "TableViewCell.h"ui
#import <AVFoundation/AVFoundation.h>atom
#import "ViewControllerShiPin.h"url
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>spa
{.net
UITableView * myTableView;3d
NSArray * array;orm
NSMutableArray * muarray;
}
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
myTableView=[[UITableView alloc]initWithFrame:CGRectMake(10,20,355 , 647)];
[self.view addSubview:myTableView];
myTableView.dataSource=self;
myTableView.delegate=self;
array=@[@"愛你一萬年",@"稻草人之戀",@"田埂上的夢"];
muarray =[[NSMutableArray alloc]initWithCapacity:10];
[self imageRequest:13.0];
NSLog(@"%@",muarray);
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return array.count;
}
-(UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell==nil) {
cell=[[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.myimage.image=muarray[indexPath.row];
cell.label.text=array[indexPath.row];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
-(void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath{
ViewControllerShiPin * shipin=[[ViewControllerShiPin alloc]init];
[self presentViewController:shipin animated:YES completion:nil];
NSUserDefaults * chuanzhi=[NSUserDefaults standardUserDefaults];
[chuanzhi setValue:array[indexPath.row] forKey:@"電影"];
}
//提取視頻圖片
-(void)imageRequest:(CGFloat)timeBySecond{
//for循環 循環出來後將每個image添加進可變的數組中
for (int i=0; i<array.count; i++) {
NSString * strurl=[[NSBundle mainBundle]pathForResource:array[i] ofType:@"mov"];
NSURL *url=[NSURL fileURLWithPath:strurl ];
AVURLAsset * urlAsset=[AVURLAsset assetWithURL:url];
AVAssetImageGenerator *imageGenerator=[AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset];
//建立截取時間點 (參數:視頻第幾秒,每秒的幀數)
CMTime time=CMTimeMakeWithSeconds(timeBySecond, 10);
//接收實際截取圖片時間
CMTime actualTime;
CGImageRef CGImage = [imageGenerator copyCGImageAtTime:time actualTime:&actualTime error:nil];
//顯示實際時間
CMTimeShow(actualTime);
//將CGImage-》UIimage
UIImage * image=[[UIImage alloc]initWithCGImage:CGImage];
[muarray addObject:image];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
———————————————————————————————————————————————————————————————
XIB.M文件
#import "TableViewCell.h"
#define W 80
#define H W
#define X 10
#define Y 10
@implementation TableViewCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
NSArray * array=[[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:nil options:nil];
self=[array lastObject];
[self layout];
return self;
}
-(void)layout{
_myimage=[[UIImageView alloc]initWithFrame:CGRectMake(X, Y, W, H)];
[self addSubview:_myimage];
_myimage.backgroundColor=[UIColor redColor];
_label =[[UILabel alloc]initWithFrame:CGRectMake(150, 50, 100, 20)];
[self addSubview:_label];
_label.textAlignment=NSTextAlignmentNatural;
_label.textColor=[UIColor orangeColor];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
———————————————————————————————————————————————————————————————
跳轉以後的.m文件
//
// ViewControllerShiPin.m
// 視頻播放器
//
// Created by DC017 on 15/12/29.
#import "ViewControllerShiPin.h"
#import <MediaPlayer/MediaPlayer.h>
@interface ViewControllerShiPin ()
@property(nonatomic,strong)MPMoviePlayerController *moviePlayerController;
@end
@implementation ViewControllerShiPin
- (void)viewDidLoad {
[super viewDidLoad];
[self.moviePlayerController play];
self.view.backgroundColor=[UIColor whiteColor];
UIButton * button=[[UIButton alloc]initWithFrame:CGRectMake(0, 20, 60, 20)];
[button setTitle:@"返回" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor]forState:UIControlStateNormal];
[button setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(fanhui) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// Do any additional setup after loading the view.
}
-(MPMoviePlayerController *)moviePlayerController{
if (!_moviePlayerController) {
NSUserDefaults * jieshou=[NSUserDefaults standardUserDefaults];
NSString * strurl=[[NSBundle mainBundle] pathForResource:[jieshou valueForKey:@"電影"] ofType:@"mov"];
NSURL *url=[NSURL fileURLWithPath:strurl];
_moviePlayerController=[[MPMoviePlayerController alloc]initWithContentURL:url];
_moviePlayerController.view.frame=self.view.frame;
//自適應屏幕
[self.view addSubview:_moviePlayerController.view];
_moviePlayerController.view.autoresizingMask=UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
}
return _moviePlayerController;
}
-(void)fanhui{
[_moviePlayerController stop];
ViewController * viewController=[[ViewController alloc]init];
[self presentViewController:viewController animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end