From 475605c931bc6f7b8e2558c295d8389d6968b1f6 Mon Sep 17 00:00:00 2001 From: dmcBig Date: Thu, 14 Jun 2018 16:51:38 +0800 Subject: [PATCH] add select album list view --- src/ios/DMCMediaPicker/AlbumListView.h | 31 +++ src/ios/DMCMediaPicker/AlbumListView.m | 140 +++++++++++ .../DMCMediaPicker/DmcPickerViewController.m | 225 ++++++++++++++---- .../dmcPicker.bundle/down_arrow.png | Bin 0 -> 571 bytes .../dmcPicker.bundle/up_arrow.png | Bin 0 -> 416 bytes 5 files changed, 346 insertions(+), 50 deletions(-) create mode 100644 src/ios/DMCMediaPicker/AlbumListView.h create mode 100644 src/ios/DMCMediaPicker/AlbumListView.m create mode 100644 src/ios/DMCMediaPicker/dmcPicker.bundle/down_arrow.png create mode 100644 src/ios/DMCMediaPicker/dmcPicker.bundle/up_arrow.png diff --git a/src/ios/DMCMediaPicker/AlbumListView.h b/src/ios/DMCMediaPicker/AlbumListView.h new file mode 100644 index 0000000..2d1a5d9 --- /dev/null +++ b/src/ios/DMCMediaPicker/AlbumListView.h @@ -0,0 +1,31 @@ +// +// AlbumListView.h +// IOSMedaiPicker +// + +#import + +@interface AlbumListView : UIView + +/** + 所有的相册数据源 + */ +@property (nonatomic ,retain)NSMutableArray * dataSource; + +/** + 所有的相册Name数据源 + */ +@property (nonatomic ,retain)NSMutableArray * dataNameSource; + +/** + select index + */ +@property (nonatomic ,assign)NSInteger* nowIndex; + +/** + 选择相册之后、传递相册数据源 + */ +@property (nonatomic ,copy)void(^didSelectAlbumBlock)(NSInteger *index); + +-(void)setListDataSource:(NSMutableArray *)dataSource dataNameSource:(NSMutableArray *)names nowSelectAlbum:(NSInteger*)nowIndex ; +@end diff --git a/src/ios/DMCMediaPicker/AlbumListView.m b/src/ios/DMCMediaPicker/AlbumListView.m new file mode 100644 index 0000000..57b33b5 --- /dev/null +++ b/src/ios/DMCMediaPicker/AlbumListView.m @@ -0,0 +1,140 @@ +// +// AlbumListView.m +// IOSMedaiPicker +// + +#import "AlbumListView.h" +#import +@interface AlbumListView () + + +@property (nonatomic ,weak)UITableView * tableView; + +//cell高度 +@property (nonatomic ,assign)CGFloat cellHeight; + +@end + +@implementation AlbumListView + +#pragma mark - 懒加载 + + +-(UITableView *)tableView +{ + if (!_tableView) { + self.cellHeight=self.bounds.size.width/5; + UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) style:UITableViewStyleGrouped]; + + [tableView setBackgroundColor:[UIColor whiteColor]]; + tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLineEtched; + tableView.estimatedRowHeight = 0; + tableView.estimatedSectionHeaderHeight = 0; + tableView.estimatedSectionFooterHeight = 0; + tableView.delegate=self; + tableView.dataSource=self; + + [self addSubview:tableView]; + _tableView=tableView; + } + return _tableView; +} + + +#pragma mark - setter +-(void)setListDataSource:(NSMutableArray *)dataSource dataNameSource:(NSMutableArray *)names nowSelectAlbum:(NSInteger*)nowIndex +{ + _dataSource=dataSource; + _dataNameSource=names; + _nowIndex=nowIndex; + [self.tableView reloadData]; +} + + + +#pragma mark - tableView的代理方法 + +-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section +{ + return self.dataSource.count; +} + + +-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath +{ + static NSString *cellString = @"cellString";//cell的重用 + UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellString]; + if (cell == nil) + { + cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellString]; + CGFloat margin=20; + UIImageView *cellImageView = [[UIImageView alloc]initWithFrame:CGRectMake(margin, 10, _cellHeight-20, _cellHeight-20)]; + cellImageView.backgroundColor = [UIColor brownColor]; + cellImageView.tag = 101; + [cell addSubview:cellImageView]; + + UILabel *cellText = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(cellImageView.frame)+margin, 0, self.bounds.size.width, _cellHeight)]; + cellText.tag = 102; + [cell addSubview:cellText]; + } + + if(_nowIndex==indexPath.item){ + cell.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.2f]; + }else{ + cell.backgroundColor=[UIColor whiteColor]; + } + + //图标 + NSString *titileName = _dataNameSource[indexPath.item]; + NSString *count = [NSString stringWithFormat:@"%ld",[_dataSource[indexPath.item] count]]; + PHAsset *asset=_dataSource[indexPath.item][0]; + [[PHImageManager defaultManager] requestImageForAsset:asset + targetSize:CGSizeMake(200 , 200) + contentMode:PHImageContentModeAspectFill + options:nil + resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) { + UIImageView *cellImageView = (UIImageView *)[cell viewWithTag:101]; + cellImageView.image = result; + //标题 + UILabel *cellText = (UILabel *)[cell viewWithTag:102]; + //创建一个带有属性的字符串比如说颜色,字体等文字的属性 + NSString * str=[NSString stringWithFormat:@"%@ %@",titileName,count]; + NSMutableAttributedString * attrStr=[[NSMutableAttributedString alloc]initWithString:str]; + + [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:[str rangeOfString:titileName]]; + + [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:[str rangeOfString:count]]; + cellText.attributedText =attrStr; + + }]; + + return cell; +} + +-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath +{ + return _cellHeight; +} + +-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section +{ + return 0.00001; +} + +-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section +{ + return 0.00001; +} + + +-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath +{ + + if (self.didSelectAlbumBlock) { + self.didSelectAlbumBlock(indexPath.item); + } + +} + + +@end diff --git a/src/ios/DMCMediaPicker/DmcPickerViewController.m b/src/ios/DMCMediaPicker/DmcPickerViewController.m index e1bed46..5578147 100644 --- a/src/ios/DMCMediaPicker/DmcPickerViewController.m +++ b/src/ios/DMCMediaPicker/DmcPickerViewController.m @@ -3,13 +3,22 @@ #import "DmcPickerViewController.h" #import "CollectionViewCell.h" #import "PreviewViewController.h" +#import "AlbumListView.h" #import #define fDeviceWidth ([UIScreen mainScreen].bounds.size.width) //设备高度的宏 #define fDeviceHeight ([UIScreen mainScreen].bounds.size.height) @interface DmcPickerViewController (){ UIBarButtonItem *preview; int litemCount; - UICollectionViewFlowLayout *flowLayout ; + UICollectionViewFlowLayout *flowLayout ; + UILabel * titleNameLabel; + UIView * titleView; + UIButton * titleArrow; + UIView * darkView; + AlbumListView *albumlistView; + NSMutableArray *albumsTitlelist; + NSMutableArray * dataSource; + NSInteger* nowSelectAlbum; } @property (strong, nonatomic) PHImageManager *manager; @@ -29,12 +38,14 @@ } -(void)initView{ + self.view.backgroundColor=[UIColor whiteColor]; UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done",nil) style:UIBarButtonItemStylePlain target:self action:@selector(done)]; UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",nil) style:UIBarButtonItemStylePlain target:self action:@selector(cancel)]; self.navigationItem.rightBarButtonItem = rightButtonItem; - self.navigationItem.leftBarButtonItem=leftButtonItem; - self.navigationItem.title=NSLocalizedString(@"All",nil); + + + //[self setTitleView:self.selectMode==102?NSLocalizedString(@"Video",nil):NSLocalizedString(@"All",nil)]; //bottom bar [self.navigationController setToolbarHidden:NO animated:YES]; @@ -43,6 +54,90 @@ [self setBtnStatus]; [self.view addSubview:self.collectionView]; } + +-(void)setTitleView:(NSString *) title{ + + float arrowHeight=self.navigationController.navigationBar.frame.size.height/3; + if(!titleNameLabel){ + titleNameLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, self.navigationController.navigationBar.frame.size.height)]; + titleNameLabel.textColor=[UIColor blackColor]; + titleNameLabel.font=[UIFont systemFontOfSize:17.5]; + titleNameLabel.text=title; + [titleNameLabel sizeToFit]; + titleView=[[UIView alloc]init]; + + titleNameLabel.center=CGPointMake(titleNameLabel.bounds.size.width/2,titleView.bounds.size.height/2); + [titleView addSubview:titleNameLabel]; + + titleArrow=[UIButton buttonWithType:UIButtonTypeCustom]; + titleArrow.frame=CGRectMake(titleNameLabel.frame.size.width+2, 0, arrowHeight, arrowHeight); + titleArrow.userInteractionEnabled=NO; + [titleArrow setImage:[UIImage imageNamed:@"dmcPicker.bundle/down_arrow.png"] forState:UIControlStateNormal]; + [titleArrow setImage:[UIImage imageNamed:@"dmcPicker.bundle/up_arrow.png"] forState:UIControlStateSelected]; + [titleView addSubview:titleArrow]; + titleArrow.center=CGPointMake(titleNameLabel.frame.size.width+arrowHeight/2+2,titleView.bounds.size.height/2); + + //添加点击手势 + UITapGestureRecognizer * tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(titleTap:)]; + [titleView addGestureRecognizer:tapGesture]; + self.navigationItem.titleView=titleView; + } + titleNameLabel.text=title; + [titleNameLabel sizeToFit]; + + titleView.frame=CGRectMake(0, 0, titleNameLabel.frame.size.width+arrowHeight+2, self.navigationController.navigationBar.frame.size.height); + titleNameLabel.center=CGPointMake(titleNameLabel.bounds.size.width/2,titleView.bounds.size.height/2); + titleArrow.frame=CGRectMake(titleNameLabel.frame.size.width+2, 0, arrowHeight, arrowHeight); + titleArrow.center=CGPointMake(titleNameLabel.frame.size.width+arrowHeight/2+2,titleView.bounds.size.height/2); +} + +- (void)titleTap:(UITapGestureRecognizer *)tap { + + if(!albumlistView){ + CGFloat y=self.navigationController.navigationBar.frame.origin.y+self.navigationController.navigationBar.frame.size.height; + darkView=[[UIView alloc]initWithFrame:CGRectMake(0, y, self.navigationController.navigationBar.frame.size.width, fDeviceHeight)]; + darkView.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.4f]; + UITapGestureRecognizer * tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(darkViewTap:)]; + [darkView addGestureRecognizer:tapGesture]; + [self.view addSubview:darkView]; + albumlistView=[[AlbumListView alloc]initWithFrame:CGRectMake(0, y, self.navigationController.navigationBar.frame.size.width, fDeviceHeight*0.6)]; + [albumlistView setListDataSource:dataSource dataNameSource:albumsTitlelist nowSelectAlbum:nowSelectAlbum]; + __weak DmcPickerViewController* weakSelf = self; + //设置选择相册之后的block回调 + [albumlistView setDidSelectAlbumBlock:^(NSInteger *index) { + + [weakSelf show:index]; + + }]; + [self.view addSubview:albumlistView]; + titleArrow.selected=YES; +// albumlist.transform = CGAffineTransformScale(CGAffineTransformIdentity,self.navigationController.navigationBar.frame.size.width, CGFLOAT_MIN); +// [UIView animateWithDuration:0.8 animations:^{ +// albumlist.transform = CGAffineTransformScale(CGAffineTransformIdentity,self.navigationController.navigationBar.frame.size.width, 1.0); +// }]; +// self.navigationController.toolbar.alpha=0.4; + [self.navigationController setToolbarHidden:YES animated:YES]; + + }else{ + [self hiddenAlbumlistView]; + } +} + +- (void)darkViewTap:(UITapGestureRecognizer *)tap { + darkView.hidden=YES; + [self hiddenAlbumlistView]; +} + + +-(void)hiddenAlbumlistView{ + titleArrow.selected=NO; + [darkView setHidden:YES]; + [albumlistView setHidden:YES]; + albumlistView=nil; + darkView=nil; + [self.navigationController setToolbarHidden:NO animated:YES]; +} + -(void) preview{ PreviewViewController * dmc=[[PreviewViewController alloc] init]; @@ -86,8 +181,19 @@ -(void) getAlassetData{ selectArray=[[NSMutableArray alloc] init]; + albumsTitlelist=[[NSMutableArray alloc] init]; + dataSource=[[NSMutableArray alloc] init]; + + + //获取相册 + PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum + subtype:PHAssetCollectionSubtypeAlbumRegular options:nil]; + PHFetchResult *syncedAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum + subtype:PHAssetCollectionSubtypeAlbumSyncedAlbum options:nil]; + PHFetchResult *userCollections = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil]; + + NSArray *collectionsFetchResults = @[smartAlbums, userCollections, syncedAlbums]; - // 获取所有资源的集合,并按资源的创建时间排序 PHFetchOptions *options = [[PHFetchOptions alloc] init]; options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]]; if(self.selectMode==100){ @@ -95,12 +201,32 @@ }else if(self.selectMode==102){ options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeVideo]; } - fetchResult = [PHAsset fetchAssetsWithOptions:options]; - + for (int i = 0; i < collectionsFetchResults.count; i ++) { + PHFetchResult *fetchResult = collectionsFetchResults[i]; + for (int x = 0; x < fetchResult.count; x ++) { + PHAssetCollection *collection = fetchResult[x]; + PHFetchResult *group = [PHAsset fetchAssetsInAssetCollection:collection options:options]; + if([group count]>0){ + [albumsTitlelist addObject:collection.localizedTitle]; + [dataSource addObject:group]; + } + } + } + _manager = [PHImageManager defaultManager]; - - [_collectionView reloadData]; + [self show:0]; } + +-(void)show:(NSInteger *)index{ + fetchResult = dataSource[(int)index]; + [self setTitleView:albumsTitlelist[(int)index]]; + [_collectionView reloadData]; + [self hiddenAlbumlistView]; + [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:NO]; + nowSelectAlbum=index; +} + + // TO DO //- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator { // @@ -173,55 +299,55 @@ #pragma mark - UICollectionView delegate dataSource #pragma mark 定义展示的UICollectionViewCell的个数 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section - { - return fetchResult.count; - } +{ + return fetchResult.count; +} #pragma mark 定义展示的Section的个数 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView - { - return 1; - } +{ + return 1; +} #pragma mark 每个UICollectionView展示的内容 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath - { - static NSString *identify = @"cell"; - CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath]; - [cell sizeToFit]; - PHAsset *asset=fetchResult[indexPath.item]; - [_manager requestImageForAsset:asset - targetSize:CGSizeMake(200 , 200) - contentMode:PHImageContentModeAspectFill - options:nil - resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) { - cell.imgView.image = result; - }]; - NSInteger i=[self isSelect:asset]; - if(asset.mediaType==PHAssetMediaTypeVideo){ - cell.labelL.hidden=NO; - cell.labelR.hidden=NO; - cell.labeGIF.hidden=YES; - - NSString *dtime=[NSString stringWithFormat:@"%.0f",asset.duration]; - cell.labelL.text=[@"\t"stringByAppendingString:NSLocalizedString(@"Video",nil)]; - cell.labelR.text=[[self getNewTimeFromDurationSecond:dtime.integerValue]stringByAppendingString:@"\t"]; - }else{ - NSString *fileName =[asset valueForKey:@"filename"]; - NSString * fileExtension = [fileName pathExtension]; - cell.labeGIF.hidden=[@"GIF" caseInsensitiveCompare:fileExtension]?YES:NO; - cell.labelL.hidden=YES; - cell.labelR.hidden=YES; - } +{ + static NSString *identify = @"cell"; + CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath]; + [cell sizeToFit]; + PHAsset *asset=fetchResult[indexPath.item]; + [_manager requestImageForAsset:asset + targetSize:CGSizeMake(200 , 200) + contentMode:PHImageContentModeAspectFill + options:nil + resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) { + cell.imgView.image = result; + }]; + NSInteger i=[self isSelect:asset]; + if(asset.mediaType==PHAssetMediaTypeVideo){ + cell.labelL.hidden=NO; + cell.labelR.hidden=NO; + cell.labeGIF.hidden=YES; - if(i<0){ - [self hidenSelectView:cell]; - }else{ - [self showSelectView:cell]; - } - return cell; + NSString *dtime=[NSString stringWithFormat:@"%.0f",asset.duration]; + cell.labelL.text=[@"\t"stringByAppendingString:NSLocalizedString(@"Video",nil)]; + cell.labelR.text=[[self getNewTimeFromDurationSecond:dtime.integerValue]stringByAppendingString:@"\t"]; + }else{ + NSString *fileName =[asset valueForKey:@"filename"]; + NSString * fileExtension = [fileName pathExtension]; + cell.labeGIF.hidden=[@"GIF" caseInsensitiveCompare:fileExtension]?YES:NO; + cell.labelL.hidden=YES; + cell.labelR.hidden=YES; } + if(i<0){ + [self hidenSelectView:cell]; + }else{ + [self showSelectView:cell]; + } + return cell; +} + //UICollectionView被选中时调用的方法 -( void )collectionView:( UICollectionView *)collectionView didSelectItemAtIndexPath:( NSIndexPath *)indexPath{ @@ -328,5 +454,4 @@ return newTime; } - - @end +@end diff --git a/src/ios/DMCMediaPicker/dmcPicker.bundle/down_arrow.png b/src/ios/DMCMediaPicker/dmcPicker.bundle/down_arrow.png new file mode 100644 index 0000000000000000000000000000000000000000..8573ff993d692e055519772e40e1b8c5759e467e GIT binary patch literal 571 zcmeAS@N?(olHy`uVBq!ia0vp^Mj*_=3?wxlRx|@CmUKs7M+SzC{oH>NS%G|oWRD45bDP46hOx7_4S6Fo+k-*%fF5lt>8h32_C|#xu`oH0%VS z`kh+!TQuu;02vzfyEW@~g4w&YKx~L0P}vU6rtLt*nm|+!Vle#w&rn|vWM~0(0cAnv z>@cq11Z4dG|NmXysS7|ms%TTISVX+WH|MS;tg&E$u zbJpy;`irS@hV*ov;)qv3N2->%MwFx^mZVxG7o`Fz1|tJQ3ta;!lvI6;>1s;*b3=DjSL74G){)!Z!22)QL$B+!?x0m*N9dQsi{xNjJN6s}mslunL znFZ$``Bk-=(`=iPnb@t{T?M<#*C%A=YTGqT&5%iD<14=SS?}bzL+_cR-`L;K?_>tr z&`{I+BJZ|f?kN-JKNbED_pErdd;KAA&yNmeMxR6jk7oC+DE;tf>w=RD=bSju)bS^& zuJb~o{W8_2>+`s|pzlw#?SI|LyTVf{M%Myq%${Jmu_+dN++|Lv1$= zX~S%{6kEeZ|+L z9+wqUlYcz8`Df|-i5gB@kInJ>_S9qY`a`$>v|bU+O5{1peKYUTS@&$gRf5iiI?=xK z(yC>O4~y%0@47!R)%ZuQ`=98$pDZL}ZF5e7f}7!h(VX8bUHvmEjug+<0||J#`njxg HN@xNAQIf4Q literal 0 HcmV?d00001