一.创建浏览界面的Cell
1.创建XLPictureShowCell类,并同时创建Xib文件
2.Xib文件布局,设置标识符以及关联控件
3.外界定义一个变量接受数据,内部重写变量的set方法显示数据
@interface XLPictureShowCell : UICollectionViewCell
/**接受一个资源模型*/
@property (nonatomic,strong) XLPictureModel *pictureModel;
@end
-(void)setPictureModel:(XLPictureModel *)pictureModel{
_pictureModel = pictureModel;
//显示图片
self.iconImageView.image = pictureModel.orgImage;
//判断是不是视频
if (pictureModel.duration > 0) {
//视频
self.videoImageView.hidden = NO;
}else{
//图片
self.videoImageView.hidden = YES;
}
}
二.创建浏览界面
1.创建一个XLPictureShowController类,继承于XLBaseViewController
2.在外部定义两个变量,一个用来接受显示的数据,一个用来接受具体显示那一页
@interface XLPictureShowController : XLBaseViewController
/**接受外部传递的模型数组*/
@property (nonatomic,strong) NSMutableArray *pictureModelArray;
/**接受外部传递的索引值用于显示哪一个*/
@property (nonatomic,assign) NSInteger index;
@end
3.界面的具体实现
@interface XLPictureShowController ()<UICollectionViewDelegate,UICollectionViewDataSource>
/**collection的容器视图*/
@property (nonatomic,strong) UICollectionView *collectionView;
@end
@implementation XLPictureShowController
#pragma mark -------界面加载 ---------
- (void)viewDidLoad {
[super viewDidLoad];
//防止循环引用
__weak typeof(self) weakSelf = self;
//返回按钮
[self addItemWithName:@"Back" postion:kButtonPostionTypeLeft complish:^(UIButton *item) {
//移除视图
[weakSelf dismissViewControllerAnimated:NO completion:nil];
}];
//collection布局
[self collectionViewInit];
}
#pragma mark -------界面显示 ---------
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
//让collection滚动到相应的页面
//1.创建IndexPath
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_index-1 inSection:0];
//2.横向滚动到indexPath对应的item
[_collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}
#pragma mark -------UICollectionView 布局 ---------
-(void)collectionViewInit{
//创建一个布局
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//横向滚动
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//item的大小
flowLayout.itemSize = CGSizeMake( self.view.width, self.view.height-self.navigationController.navigationBar.bottom);
//item之间的横向距离
flowLayout.minimumLineSpacing = 0;
//创建
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.navigationController.navigationBar.bottom, self.view.width, self.view.height-self.navigationController.navigationBar.bottom) collectionViewLayout:flowLayout];
//按页显示
_collectionView.pagingEnabled = YES;
//不反弹
_collectionView.bounces = NO;
//隐藏横向滚动条
_collectionView.showsHorizontalScrollIndicator = NO;
//代理
_collectionView.delegate = self;
_collectionView.dataSource = self;
//显示
[self.view addSubview:_collectionView];
//注册
[_collectionView registerNib:[UINib nibWithNibName:@"XLPictureShowCell" bundle:nil]
forCellWithReuseIdentifier:@"cellID"];
}
#pragma mark -------UICollectionView 代理方法 ---------
//item数量
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.pictureModelArray.count;
}
//item显示什么
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//创建
XLPictureShowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
//给数据
cell.pictureModel = self.pictureModelArray[indexPath.row];
return cell;
}
//item被点击
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
}
4.视频的播放,由于每次重新运行程序程序的Documents文件夹路径会发生改变,所以播放的时候需要调整videoPath
//item被点击
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//取得对应的模型
XLPictureModel *model = self.pictureModelArray[indexPath.row];
//判断是不是视频
if (model.duration > 0) {
//视频
//每一次开启的时候Documents路径不同 需要转化
NSString *str = model.videoPath.absoluteString;
//截取信息
NSRange range = [str rangeOfString:@"/Documents/"];
//转化为需要的路径字符串
NSString *urlString = [[[XLFileManager sharedManager] docPath] stringByAppendingPathComponent:[str substringFromIndex:range.location+range.length]];
//播放
[UIView animateWithDuration:0.1 animations:^{
//获得当前的cell
XLPictureShowCell *cell = (XLPictureShowCell*)[collectionView cellForItemAtIndexPath:indexPath];
//隐藏
cell.alpha = 0;
}completion:^(BOOL finished) {
//播放器
AVPlayer *player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:urlString]];
//播放图层
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = self.collectionView.bounds;
[self.collectionView.layer insertSublayer:playerLayer atIndex:0];
//播放
[player play];
}];
}
}
5.界面的跳转
//进入浏览界面
//创建
XLPictureShowController *psc = [XLPictureShowController new];
//给数据
psc.pictureModelArray = self.pictureModelArray;
//给索引值
psc.index = indexPath.row;
//中途需要导航栏
XLNavigationController *mynav = [[XLNavigationController alloc] initWithRootViewController:psc];
//找到当前界面的视图
//1.通过窗口找到当前的界面
XLNavigationController *nav = (XLNavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
//2.找到导航栏控制器当前显示的界面
UIViewController *pictureView = nav.visibleViewController;
//跳转
[pictureView presentViewController:mynav animated:NO completion:nil];
三.检测到的问题
1.删除相册,相册内资源模型没有被删除,修改deleteAlbum方法
#pragma mark -------删除选中的相册 ---------
-(void)deleteAlbum{
//临时数组
NSMutableArray *tempArray = [NSMutableArray array];
//1.删除documents里面对应的文件夹
for (XLAlbumModel *model in self.selectedAlbumModelsArray) {
//删除目录
[[XLFileManager sharedManager] removeAlbumDirectory:model.name];
//遍历
for (XLPictureModel *picModel in [XLSystemAlbumManager sharedSystemAlbum].pictureModelArray) {
if ([picModel.albumName isEqualToString:model.name]) {
//这个资源是这个相册的
[tempArray addObject:picModel];
}
}
}
//2.删除数据模型
[self.albumModelsArray removeObjectsInArray:self.selectedAlbumModelsArray];
//3.删除相册内资源模型
[[XLSystemAlbumManager sharedSystemAlbum].pictureModelArray removeObjectsInArray:tempArray];
//4.清空选中的数组
[self.selectedAlbumModelsArray removeAllObjects];
//5.触发改变
[self modelsArrayChanged];
}
2.相册是中文名,播放路径有问题,修改didSelectItemAtIndexPath中的相关内容
//item被点击
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//取得对应的模型
XLPictureModel *model = self.pictureModelArray[indexPath.row];
//判断是不是视频
if (model.duration > 0) {
//视频
//每一次开启的时候Documents路径不同 需要转化
NSString *str = model.videoPath.absoluteString;
//截取信息
NSRange range = [str rangeOfString:@"/video/video"];
//文件名
str = [str substringFromIndex:range.location+range.length];
//转化为需要的路径字符串
NSString *urlString = [[[XLFileManager sharedManager] docPath] stringByAppendingPathComponent:model.albumName];
urlString = [urlString stringByAppendingPathComponent:@"/video/video"];
urlString = [urlString stringByAppendingFormat:@"/%@",str];
//播放
[UIView animateWithDuration:0.1 animations:^{
//获得当前的cell
XLPictureShowCell *cell = (XLPictureShowCell*)[collectionView cellForItemAtIndexPath:indexPath];
//隐藏
cell.alpha = 0;
}completion:^(BOOL finished) {
NSLog(@"NSURL:%@",[NSURL fileURLWithPath:urlString]);
//播放器
AVPlayer *player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:urlString]];
//播放图层
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = self.collectionView.bounds;
[self.collectionView.layer insertSublayer:playerLayer atIndex:0];
//播放
[player play];
}];
}
}
四.结果显示
第一次进入程序设置密码以及相册创建重命名 第二次进入程序添加资源到个人相册 删除相册 个人相册内部资源浏览以及资源删除五.工程链接
链接:
https://pan.baidu.com/s/1a4Z87pE8HiMzNOq6O3kP6A
密码:67cf
网友评论