一.个人相册内部界面的具体显示
添加两个消息,一个用来监听数据源是否改变,一个用来监听编辑状态。数据源改变了,需要重新加载数据并刷新。编辑状态改变了,如果是不编辑的时候需要将所有的模型的isSelected复原,然后刷新数据让Cell重新显示
在StringConstants里面加入编辑按钮被点击的消息
//个人相册界面编辑状态改变的消息
#define kPictureEditStatusNotification @"kPictureEditStatusNotification"
@interface XLPictureCollectionView ()<UICollectionViewDelegate,UICollectionViewDataSource>
/**接受传递的类型*/
@property (nonatomic,assign) ViewType type;
/**接受传递的相册模型*/
@property (nonatomic,strong) XLAlbumModel *albumModel;
/**保存我们显示的资源模型*/
@property (nonatomic,strong) NSMutableArray *pictureModelArray;
/**属性化的collectionView*/
@property (nonatomic,strong) UICollectionView *collectionView;
/**记录编辑状态*/
@property (nonatomic,assign) BOOL isEditing;
@end
@implementation XLPictureCollectionView
#pragma mark -------移除消息 ---------
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark -------类方法创建 ---------
+(XLPictureCollectionView *)showCollectionView:(CGRect)frame ViewType:(ViewType)type Model:(XLAlbumModel *)model toView:(UIView *)superView{
//创建
XLPictureCollectionView *pcv = [[XLPictureCollectionView alloc] initWithFrame:frame];
//保存类型
pcv.type = type;
//保存传递过来的相册模型
pcv.albumModel = model;
//保存数据
if (type == ViewTypeSystem) {
//加载系统相册的图片
}else{
//加载个人相册的图片
//创建一个数组保存当前相册的资源
NSMutableArray *tempArray = [NSMutableArray array];
//遍历
for (XLPictureModel *picModel in [XLSystemAlbumManager sharedSystemAlbum].pictureModelArray) {
//判断资源是不是当前相册的
if ([picModel.albumName isEqualToString:model.name]) {
[tempArray addObject:picModel];
}
}
//显示
pcv.pictureModelArray = tempArray;
}
//显示
[superView addSubview:pcv];
return pcv;
}
#pragma mark -------重写initWithFrame方法 布局 ---------
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
//当前视图的背景颜色
self.backgroundColor = [UIColor clearColor];
//创建布局对象
UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
//每一个item的大小
layout.itemSize = CGSizeMake(kSize, kSize);
//竖向最小间距
layout.minimumLineSpacing = 1;
//横向最小间距
layout.minimumInteritemSpacing = 0.001f;
//layout.sectionInset = UIEdgeInsetsMake(5, 5, 5,5);
//创建collectionView
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) collectionViewLayout:layout];
//collectionView的背景颜色
_collectionView.backgroundColor = [UIColor clearColor];
//设置代理
_collectionView.delegate = self;
_collectionView.dataSource = self;
//显示
[self addSubview:_collectionView];
//注册
[_collectionView registerNib:[UINib nibWithNibName:@"XLPictureCell" bundle:nil] forCellWithReuseIdentifier:@"cellID"];
}
return self;
}
#pragma mark -------重写type的set方法 ---------
-(void)setType:(ViewType)type{
_type = type;
if (type == ViewTypePerson) {
//监听数据源改变的消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataSourceChanged) name:kPictureModelsArrarChangeNotificationName object:nil];
//监听编辑状态
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editStatusChanged:) name:kPictureEditStatusNotification object:nil];
}
}
#pragma mark -------数据源改变刷新数据 ---------
-(void)dataSourceChanged{
//创建一个数组保存当前相册的资源
NSMutableArray *tempArray = [NSMutableArray array];
//遍历
for (XLPictureModel *picModel in [XLSystemAlbumManager sharedSystemAlbum].pictureModelArray) {
//判断资源是不是当前相册的
if ([picModel.albumName isEqualToString:self.albumModel.name]) {
[tempArray addObject:picModel];
}
}
//显示
self.pictureModelArray = tempArray;
//刷新
[_collectionView reloadData];
}
#pragma mark -------编辑状态改变 ---------
-(void)editStatusChanged:(NSNotification *)noti{
//接受消息传递过来的编辑状态
self.isEditing = [noti.object boolValue];
//不编辑的时候
if (self.isEditing == NO) {
//状态恢复
for (XLPictureModel *model in self.pictureModelArray) {
model.isSelected = NO;
}
}
//刷新
[_collectionView reloadData];
}
#pragma mark -------UICollectionView代理方法 ---------
//返回item的数量
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//判断是什么类型
if (self.type == ViewTypePerson) {
//个人相册
return self.pictureModelArray.count+1;
}else{
//系统相册
return self.pictureModelArray.count;
}
}
//每个cell显示什么
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//创建
XLPictureCell *cell;
//判断类型
if (self.type == ViewTypePerson) {
//个人相册
//判断是不是第一个cell被点击了
if (indexPath.row == 0) {
//初始化cell
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
//cell的类型
cell.type = CelllTypeAdd;
}else{
//初始化cell
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
//传递数据
cell.model = self.pictureModelArray[indexPath.row-1];
//cell的类型
cell.type = CellTypePerson;
//编辑状态
cell.isEditing = self.isEditing;
}
}else{
//系统相册
}
return cell;
}
//item被点击了
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//判断类型
if (self.type == ViewTypePerson) {
//个人相册
//判断是不是第一个被点击了
if (indexPath.row == 0) {
}else{
//第一个之外的被点击了
//判断是不是编辑状态
if (self.isEditing) {
//编辑状态
}else{
//进入浏览界面
//创建
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];
}
}
}else{
//系统相册
@end
二.重命名功能的修改
重命名的时候,视频的路径被更改了,需要调用replaceOccurrencesOfString方法修改模型的videoPath
#pragma mark -------重命名相册 ---------
-(BOOL)renameAlbum:(NSString *)newName WithOldName:(NSString *)oldName{
//1.判断相册是否存在
if ([self isAlbumExist:newName]) {
//已经存在了
return NO;
}
//For in实际上是简单的快速枚举,不具有实时监测数组容器变化的功能。所以对于可变数组进行枚举操作时,不能通过添加或删除对象等这类操作来改变数组容器,否则就会报错
//更改相册模型里面的内容
for (XLAlbumModel *model in self.albumModelsArray) {
//发现有重复的
if ([model.name isEqualToString:oldName]) {
model.name = newName;
}
}
//更改资源模型里面的内容
for (XLPictureModel *model in [XLSystemAlbumManager sharedSystemAlbum].pictureModelArray) {
//发现有重复的
if ([model.albumName isEqualToString:oldName]) {
model.albumName = newName;
//获得字符串
NSMutableString *string = [NSMutableString stringWithString:model.videoPath.absoluteString];
//比较字符串的范围
NSRange range = NSMakeRange(0, [string length]);
//替换
[string replaceOccurrencesOfString:oldName withString:newName options:NSLiteralSearch range:range];
//重新设置URL
model.videoPath = [NSURL URLWithString:string];
}
}
//更改文件里面的内容
[[XLFileManager sharedManager] changeFolderName:newName beforeName:oldName];
//触发改变
[self modelsArrayChanged];
[[XLSystemAlbumManager sharedSystemAlbum] modelsArrayChanged];
return YES;
}
三.删除个人相册资源的功能
1.首先在XLFileManager里面添加删除某个资源的方法
#pragma mark -------删除资源 ---------
-(void)deletetheFileName:(NSString *)fileName WithAlbumName:(NSString *)albumName IsVideo:(BOOL)isVideo{
//判断是不是视频
if (isVideo == YES) {
//视频
//获得小图路径
NSString *smallPath = [self filePathWith:albumName mediaType:MediaTypeVideo pictureType:PictureTypeSmall file:fileName];
[[NSFileManager defaultManager] removeItemAtPath:smallPath error:nil];
//获得原图路径
NSString *orgPath = [self filePathWith:albumName mediaType:MediaTypeVideo pictureType:PictureTypeOrg file:fileName];
[[NSFileManager defaultManager] removeItemAtPath:orgPath error:nil];
//获得视频路径
NSString *vidPath = [self filePathWith:albumName mediaType:MediaTypeVideo pictureType:PictureTypeOrg file:fileName];
[[NSFileManager defaultManager] removeItemAtPath:vidPath error:nil];
}else{
//图片
//获得小图路径
NSString *smallPath = [self filePathWith:albumName mediaType:MediaTypePicture pictureType:PictureTypeSmall file:fileName];
[[NSFileManager defaultManager] removeItemAtPath:smallPath error:nil];
//获得原图路径
NSString *orgPath = [self filePathWith:albumName mediaType:MediaTypePicture pictureType:PictureTypeOrg file:fileName];
[[NSFileManager defaultManager] removeItemAtPath:orgPath error:nil];
}
}
2.在XLAlbumManager里面添加删除相册内某个资源的方法,并在其中,更新相册模型。最重要的是不要忘了清空临时数组
#pragma mark -------删除选中的资源 ---------
-(void)deletePicture{
//遍历
for (XLPictureModel *model in self.selectedPictureModelArray) {
//资源数组删除
[[XLSystemAlbumManager sharedSystemAlbum].pictureModelArray removeObject:model];
//文件内部删除
if (model.duration > 0) {
//视频
[[XLFileManager sharedManager] deletetheFileName:model.name WithAlbumName:model.albumName IsVideo:YES];
}else{
//图片
[[XLFileManager sharedManager] deletetheFileName:model.name WithAlbumName:model.albumName IsVideo:NO];
}
//改变相册模型数据
for (XLAlbumModel *albumModel in self.albumModelsArray) {
//找到这个相册
if ([model.albumName isEqualToString:albumModel.name]) {
//更改数量
if (model.duration > 0) {
//视频数量
albumModel.videoNumber--;
}else{
//图片数量
albumModel.photoNumber--;
}
//更换图片
if (albumModel.videoNumber == 0 && albumModel.photoNumber == 0) {
albumModel.icon = [UIImage imageNamed:@"Album_BG_1-99"];
}
}
}
}
//触发改变
[self modelsArrayChanged];
[[XLSystemAlbumManager sharedSystemAlbum] modelsArrayChanged];
//清空选中的数组
[self.selectedPictureModelArray removeAllObjects];
}
3.和之前的相同,在XLAlbumManager中定义一个数组selectedPictureModelArray用来临时保存被选中的资源
/**数据源-保存选中的相册的资源*/
@property (nonatomic,strong) NSMutableArray *selectedPictureModelArray;
//选中的资源模型数组
-(NSMutableArray *)selectedPictureModelArray{
if (_selectedPictureModelArray == nil) {
//初始化
self.selectedPictureModelArray = [NSMutableArray array];
}
return _selectedPictureModelArray;
}
4.同样的点击item更改对应模型的isSelected并且调用changeStatus:方法更爱勾选按钮的状态,通过模型的isSelected判断是否加入到临时数组中
//判断是不是编辑状态
if (self.isEditing) {
//编辑状态
//获取indexPath.row对应的model
XLPictureModel *model = self.pictureModelArray[indexPath.row-1];
//更改isSelected的状态
model.isSelected = !model.isSelected;
//获取当前的cell
XLPictureCell *cell = (XLPictureCell*)[collectionView cellForItemAtIndexPath:indexPath];
//将状态传递给cell
[cell changeStatus:model.isSelected];
//判断资源有没有被选中
if (model.isSelected == YES) {
//加入临时资源数组中
[[XLAlbumManager sharedManager].selectedPictureModelArray addObject:model];
}else{
//从临时资源数组中移除
[[XLAlbumManager sharedManager].selectedPictureModelArray removeObject:model];
}
}
5.具体执行删除的地方,多选,拷贝,剪切功能和其它功能类似,留在这里,以后优化的时候再添加
//创建显示工具条
[weakSelf showToolBarWithType:ToolBarTypeAlbum handle:^(NSInteger index) {
if (index == 1) {
//多选
}else if(index == 2){
//拷贝
}else if(index == 3){
//剪切
}else{
//删除
[[XLAlbumManager sharedManager] deletePicture];
}
}];
网友评论