From 546f98dfd10c41cfedbd7f8bd43b16e87be2ec6b Mon Sep 17 00:00:00 2001 From: Roger Guo Date: Tue, 13 Nov 2018 08:50:19 +0800 Subject: [PATCH] Let the default selected album to be CameraRoll. Fixing index from NSInteger* to NSInteger. https://github.com/DmcSDK/cordova-plugin-mediaPicker/issues/50 https://github.com/DmcSDK/cordova-plugin-mediaPicker/issues/36 --- src/ios/DMCMediaPicker/AlbumListView.h | 6 ++--- src/ios/DMCMediaPicker/AlbumListView.m | 12 ++++----- .../DMCMediaPicker/DmcPickerViewController.m | 25 +++++++++++-------- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/ios/DMCMediaPicker/AlbumListView.h b/src/ios/DMCMediaPicker/AlbumListView.h index 2d1a5d9..6a6fc66 100644 --- a/src/ios/DMCMediaPicker/AlbumListView.h +++ b/src/ios/DMCMediaPicker/AlbumListView.h @@ -20,12 +20,12 @@ /** select index */ -@property (nonatomic ,assign)NSInteger* nowIndex; +@property (nonatomic ,assign)NSInteger nowIndex; /** 选择相册之后、传递相册数据源 */ -@property (nonatomic ,copy)void(^didSelectAlbumBlock)(NSInteger *index); +@property (nonatomic ,copy)void(^didSelectAlbumBlock)(NSInteger index); --(void)setListDataSource:(NSMutableArray *)dataSource dataNameSource:(NSMutableArray *)names nowSelectAlbum:(NSInteger*)nowIndex ; +-(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 index 2590635..08462e5 100644 --- a/src/ios/DMCMediaPicker/AlbumListView.m +++ b/src/ios/DMCMediaPicker/AlbumListView.m @@ -42,18 +42,18 @@ #pragma mark - setter --(void)setListDataSource:(NSMutableArray *)dataSource dataNameSource:(NSMutableArray *)names nowSelectAlbum:(NSInteger*)nowIndex +-(void)setListDataSource:(NSMutableArray *)dataSource dataNameSource:(NSMutableArray *)names nowSelectAlbum: (NSInteger)nowIndex { _dataSource=dataSource; _dataNameSource=names; _nowIndex=nowIndex; [self.tableView reloadData]; - int scrolltoIndex=0; + NSInteger scrolltoIndex = 0; //下面代码是让下拉选中了有个居中的感觉 - if((int)_nowIndex>[_dataNameSource count]-4){ - scrolltoIndex= [_dataNameSource count]-1; + if(_nowIndex > [_dataNameSource count]-4){ + scrolltoIndex = [_dataNameSource count]-1; }else{ - scrolltoIndex=((int)_nowIndex+3); + scrolltoIndex=(_nowIndex+3); } if([_dataNameSource count]>3){ [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:scrolltoIndex inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO]; @@ -150,4 +150,4 @@ } -@end \ No newline at end of file +@end diff --git a/src/ios/DMCMediaPicker/DmcPickerViewController.m b/src/ios/DMCMediaPicker/DmcPickerViewController.m index 5dc1f3c..469ee1d 100644 --- a/src/ios/DMCMediaPicker/DmcPickerViewController.m +++ b/src/ios/DMCMediaPicker/DmcPickerViewController.m @@ -18,7 +18,7 @@ AlbumListView *albumlistView; NSMutableArray *albumsTitlelist; NSMutableArray * dataSource; - NSInteger* nowSelectAlbum; + NSInteger nowSelectAlbum; } @property (strong, nonatomic) PHImageManager *manager; @@ -104,7 +104,7 @@ [albumlistView setListDataSource:dataSource dataNameSource:albumsTitlelist nowSelectAlbum:nowSelectAlbum]; __weak DmcPickerViewController* weakSelf = self; //设置选择相册之后的block回调 - [albumlistView setDidSelectAlbumBlock:^(NSInteger *index) { + [albumlistView setDidSelectAlbumBlock:^(NSInteger index) { [weakSelf show:index]; @@ -203,7 +203,9 @@ }else if(self.selectMode==102){ options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeVideo]; } - + + int defaultSelection = 0; + int i = 0; for (PHFetchResult *fetchResult in allAlbums) { for (PHAssetCollection *collection in fetchResult) { // 有可能是PHCollectionList类的的对象,过滤掉 @@ -212,23 +214,26 @@ if (collection.estimatedAssetCount <= 0) continue; if (collection.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumAllHidden) continue; //包含隐藏照片或视频的文件夹 if (collection.assetCollectionSubtype == 1000000201) continue; //『最近删除』相册 - + if (collection.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumUserLibrary) { + defaultSelection = i; + } PHFetchResult *group = [PHAsset fetchAssetsInAssetCollection:collection options:options]; if([group count]>0){ [albumsTitlelist addObject:collection.localizedTitle]; [dataSource addObject:group]; + i++; } - } + } } - + _manager = [PHImageManager defaultManager]; - [self show:0]; + [self show: defaultSelection]; } --(void)show:(NSInteger *)index{ +-(void)show:(NSInteger) index { if([dataSource count]>0){ - fetchResult = dataSource[(int)index]; - [self setTitleView:albumsTitlelist[(int)index]]; + fetchResult = dataSource[index]; + [self setTitleView:albumsTitlelist[index]]; [_collectionView reloadData]; [self hiddenAlbumlistView]; [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:NO];