add select album list view

This commit is contained in:
dmcBig
2018-06-14 16:51:38 +08:00
parent 89a1eb71a9
commit 475605c931
5 changed files with 346 additions and 50 deletions
+31
View File
@@ -0,0 +1,31 @@
//
// AlbumListView.h
// IOSMedaiPicker
//
#import <UIKit/UIKit.h>
@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
+140
View File
@@ -0,0 +1,140 @@
//
// AlbumListView.m
// IOSMedaiPicker
//
#import "AlbumListView.h"
#import <Photos/Photos.h>
@interface AlbumListView ()<UITableViewDelegate,UITableViewDataSource>
@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
+175 -50
View File
@@ -3,13 +3,22 @@
#import "DmcPickerViewController.h"
#import "CollectionViewCell.h"
#import "PreviewViewController.h"
#import "AlbumListView.h"
#import <Photos/Photos.h>
#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<UIViewControllerTransitionCoordinator>)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
Binary file not shown.

After

Width:  |  Height:  |  Size: 571 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 416 B