DKPhotoPickerAssetsViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. //
  2. // DKPhotoPickerAssetsViewController.m
  3. // DKAssetsPickerDemo
  4. //
  5. // Copyright (c) 2014年 com.zixue101.www. All rights reserved.
  6. //
  7. #import <AssetsLibrary/AssetsLibrary.h>
  8. #import "DKPhoto.h"
  9. #import "DKPhotoPickerCollectionView.h"
  10. #import "DKPhotoPickerGroup.h"
  11. #import "DKPhotoPickerCollectionViewCell.h"
  12. #import "DKPhotoPickerFooterCollectionReusableView.h"
  13. #import "Util.h"
  14. static CGFloat CELL_ROW = 4;
  15. static CGFloat CELL_MARGIN = 2;
  16. static CGFloat CELL_LINE_MARGIN = 2;
  17. static CGFloat TOOLBAR_HEIGHT = 44;
  18. static NSString *const _cellIdentifier = @"cell";
  19. static NSString *const _footerIdentifier = @"FooterView";
  20. static NSString *const _identifier = @"toolBarThumbCollectionViewCell";
  21. @interface DKPhotoPickerAssetsViewController () <DKPhotoPickerCollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,DKPhotoPickerBrowserViewControllerDataSource,DKPhotoPickerBrowserViewControllerDelegate>
  22. // View
  23. // 相片View
  24. @property (nonatomic , strong) DKPhotoPickerCollectionView *collectionView;
  25. // 底部CollectionView
  26. @property (nonatomic , weak) UICollectionView *toolBarThumbCollectionView;
  27. // 标记View
  28. @property (nonatomic , weak) UILabel *makeView;
  29. @property (nonatomic , strong) UIButton *doneBtn;
  30. @property (nonatomic , weak) UIToolbar *toolBar;
  31. @property (assign,nonatomic) NSUInteger privateTempMinCount;
  32. // Datas
  33. // 数据源
  34. @property (nonatomic , strong) NSMutableArray *assets;
  35. // 记录选中的assets
  36. @property (nonatomic , strong) NSMutableArray *selectAssets;
  37. @end
  38. @implementation DKPhotoPickerAssetsViewController
  39. #pragma mark - getter
  40. #pragma mark Get Data
  41. - (NSMutableArray *)selectAssets{
  42. if (!_selectAssets) {
  43. _selectAssets = [NSMutableArray array];
  44. }
  45. return _selectAssets;
  46. }
  47. #pragma mark Get View
  48. - (UIButton *)doneBtn{
  49. if (!_doneBtn) {
  50. UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  51. [rightBtn setTitleColor:[UIColor colorWithRed:0/255.0 green:91/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateNormal];
  52. [rightBtn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
  53. rightBtn.enabled = YES;
  54. rightBtn.titleLabel.font = [UIFont systemFontOfSize:17];
  55. rightBtn.frame = CGRectMake(0, 0, 45, 45);
  56. [rightBtn setTitle:@"完成" forState:UIControlStateNormal];
  57. [rightBtn addTarget:self action:@selector(done) forControlEvents:UIControlEventTouchUpInside];
  58. [rightBtn addSubview:self.makeView];
  59. self.doneBtn = rightBtn;
  60. }
  61. return _doneBtn;
  62. }
  63. - (UICollectionView *)toolBarThumbCollectionView{
  64. if (!_toolBarThumbCollectionView) {
  65. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  66. flowLayout.itemSize = CGSizeMake(40, 40);
  67. flowLayout.minimumInteritemSpacing = 0;
  68. flowLayout.minimumLineSpacing = 5;
  69. flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  70. // CGRectMake(0, 22, 300, 44)
  71. UICollectionView *toolBarThumbCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 0, self.view.width - 100, 44) collectionViewLayout:flowLayout];
  72. toolBarThumbCollectionView.backgroundColor = [UIColor clearColor];
  73. toolBarThumbCollectionView.dataSource = self;
  74. toolBarThumbCollectionView.delegate = self;
  75. [toolBarThumbCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:_identifier];
  76. self.toolBarThumbCollectionView = toolBarThumbCollectionView;
  77. [self.toolBar addSubview:toolBarThumbCollectionView];
  78. }
  79. return _toolBarThumbCollectionView;
  80. }
  81. - (void)setSelectPickerAssets:(NSArray *)selectPickerAssets{
  82. NSSet *set = [NSSet setWithArray:selectPickerAssets];
  83. _selectPickerAssets = [set allObjects];
  84. if (!self.assets) {
  85. self.assets = [NSMutableArray arrayWithArray:selectPickerAssets];
  86. }else{
  87. [self.assets addObjectsFromArray:selectPickerAssets];
  88. }
  89. for (DKPhotoAssets *assets in selectPickerAssets) {
  90. if ([assets isKindOfClass:[DKPhotoAssets class]]) {
  91. [self.selectAssets addObject:assets];
  92. }
  93. }
  94. self.collectionView.lastDataArray = nil;
  95. self.collectionView.isRecoderSelectPicker = YES;
  96. self.collectionView.selectAsstes = self.selectAssets;
  97. NSInteger count = self.selectAssets.count;
  98. self.makeView.text = [NSString stringWithFormat:@"%ld",(long)(count+self.cameraCount)];
  99. self.makeView.hidden = !(count+self.cameraCount);
  100. self.doneBtn.enabled = (count > 0);
  101. }
  102. #pragma mark collectionView
  103. - (DKPhotoPickerCollectionView *)collectionView{
  104. if (!_collectionView) {
  105. CGFloat cellW = (self.view.frame.size.width - CELL_MARGIN * CELL_ROW + 1) / CELL_ROW;
  106. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  107. layout.itemSize = CGSizeMake(cellW, cellW);
  108. layout.minimumInteritemSpacing = 0;
  109. layout.minimumLineSpacing = CELL_LINE_MARGIN;
  110. layout.footerReferenceSize = CGSizeMake(self.view.frame.size.width, TOOLBAR_HEIGHT * 2);
  111. DKPhotoPickerCollectionView *collectionView = [[DKPhotoPickerCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  112. // 时间置顶
  113. collectionView.status = DKPickerCollectionViewShowOrderStatusTimeDesc;
  114. collectionView.translatesAutoresizingMaskIntoConstraints = NO;
  115. [collectionView registerClass:[DKPhotoPickerCollectionViewCell class] forCellWithReuseIdentifier:_cellIdentifier];
  116. // 底部的View
  117. [collectionView registerClass:[DKPhotoPickerFooterCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:_footerIdentifier];
  118. collectionView.contentInset = UIEdgeInsetsMake(5, 0,TOOLBAR_HEIGHT, 0);
  119. collectionView.collectionViewDelegate = self;
  120. [self.view insertSubview:collectionView belowSubview:self.toolBar];
  121. self.collectionView = collectionView;
  122. NSDictionary *views = NSDictionaryOfVariableBindings(collectionView);
  123. NSString *widthVfl = @"H:|-0-[collectionView]-0-|";
  124. [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:widthVfl options:0 metrics:nil views:views]];
  125. NSString *heightVfl = @"V:|-0-[collectionView]-0-|";
  126. [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:heightVfl options:0 metrics:nil views:views]];
  127. }
  128. return _collectionView;
  129. }
  130. #pragma mark makeView 红点标记View
  131. - (UILabel *)makeView{
  132. if (!_makeView) {
  133. UILabel *makeView = [[UILabel alloc] init];
  134. makeView.textColor = [UIColor whiteColor];
  135. makeView.textAlignment = NSTextAlignmentCenter;
  136. makeView.font = [UIFont systemFontOfSize:13];
  137. makeView.frame = CGRectMake(-5, -5, 20, 20);
  138. makeView.hidden = YES;
  139. makeView.layer.cornerRadius = makeView.frame.size.height / 2.0;
  140. makeView.clipsToBounds = YES;
  141. makeView.backgroundColor = [UIColor redColor];
  142. [self.view addSubview:makeView];
  143. self.makeView = makeView;
  144. }
  145. return _makeView;
  146. }
  147. - (void)viewDidLoad {
  148. [super viewDidLoad];
  149. self.view.bounds = [UIScreen mainScreen].bounds;
  150. self.view.backgroundColor = [UIColor whiteColor];
  151. // 初始化按钮
  152. [self setupButtons];
  153. // 初始化底部ToorBar
  154. [self setupToorBar];
  155. }
  156. #pragma mark - setter
  157. #pragma mark 初始化按钮
  158. - (void) setupButtons{
  159. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(back)];
  160. }
  161. #pragma mark 初始化所有的组
  162. - (void) setupAssets{
  163. if (!self.assets) {
  164. self.assets = [NSMutableArray array];
  165. }
  166. __block NSMutableArray *assetsM = [NSMutableArray array];
  167. __weak typeof(self) weakSelf = self;
  168. [[DKPhotoPickerDatas defaultPicker] getGroupPhotosWithGroup:self.assetsGroup finished:^(NSArray *assets) {
  169. [assets enumerateObjectsUsingBlock:^(ALAsset *asset, NSUInteger idx, BOOL *stop) {
  170. DKPhotoAssets *DKAsset = [[DKPhotoAssets alloc] init];
  171. DKAsset.asset = asset;
  172. [assetsM addObject:DKAsset];
  173. }];
  174. weakSelf.collectionView.dataArray = assetsM;
  175. }];
  176. }
  177. #pragma mark -初始化底部ToorBar
  178. - (void) setupToorBar{
  179. UIToolbar *toorBar = [[UIToolbar alloc] init];
  180. toorBar.translatesAutoresizingMaskIntoConstraints = NO;
  181. [self.view addSubview:toorBar];
  182. self.toolBar = toorBar;
  183. NSDictionary *views = NSDictionaryOfVariableBindings(toorBar);
  184. NSString *widthVfl = @"H:|-0-[toorBar]-0-|";
  185. NSString *heightVfl = [NSString stringWithFormat:@"%@%f%@",@"V:[toorBar(44)]-",[Util obtainHomeAreaHeight],@"-|"];
  186. [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:widthVfl options:0 metrics:0 views:views]];
  187. [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:heightVfl options:0 metrics:0 views:views]];
  188. // 左视图 中间距 右视图
  189. UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:self.toolBarThumbCollectionView];
  190. UIBarButtonItem *fiexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
  191. UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:self.doneBtn];
  192. toorBar.items = @[leftItem,fiexItem,rightItem];
  193. }
  194. #pragma mark - setter
  195. -(void)setMinCount:(NSInteger)minCount{
  196. _minCount = minCount;
  197. if (!_privateTempMinCount) {
  198. _privateTempMinCount = minCount;
  199. }
  200. if (self.selectAssets.count == minCount){
  201. minCount = 0;
  202. }else if (self.selectPickerAssets.count - self.selectAssets.count > 0) {
  203. minCount = _privateTempMinCount;
  204. }
  205. self.collectionView.minCount = minCount;
  206. }
  207. -(void)setCameraCount:(NSInteger)cameraCount{
  208. _cameraCount=cameraCount;
  209. self.collectionView.cameraCount = cameraCount;
  210. }
  211. - (void)setAssetsGroup:(DKPhotoPickerGroup *)assetsGroup{
  212. if (!assetsGroup.groupName.length) return ;
  213. _assetsGroup = assetsGroup;
  214. self.title = assetsGroup.groupName;
  215. // 获取Assets
  216. [self setupAssets];
  217. }
  218. - (void) pickerCollectionViewDidSelected:(DKPhotoPickerCollectionView *) pickerCollectionView deleteAsset:(DKPhotoAssets *)deleteAssets{
  219. if (self.selectPickerAssets.count == 0){
  220. self.selectAssets = [NSMutableArray arrayWithArray:pickerCollectionView.selectAsstes];
  221. }else if (deleteAssets == nil){
  222. [self.selectAssets addObject:[pickerCollectionView.selectAsstes lastObject]];
  223. }
  224. // self.selectAssets = [NSMutableArray arrayWithArray:[[NSSet setWithArray:self.selectAssets] allObjects]];
  225. NSInteger count = self.selectAssets.count;
  226. self.makeView.hidden = !count;
  227. self.makeView.text = [NSString stringWithFormat:@"%ld",(long)(count+self.cameraCount)];
  228. self.doneBtn.enabled = (count > 0);
  229. [self.toolBarThumbCollectionView reloadData];
  230. if (self.selectPickerAssets.count || deleteAssets) {
  231. DKPhotoAssets *asset = [pickerCollectionView.lastDataArray lastObject];
  232. if (deleteAssets){
  233. asset = deleteAssets;
  234. }
  235. NSInteger selectAssetsCurrentPage = -1;
  236. for (NSInteger i = 0; i < self.selectAssets.count; i++) {
  237. DKPhotoAssets *photoAsset = self.selectAssets[i];
  238. if([[[[asset.asset defaultRepresentation] url] absoluteString] isEqualToString:[[[photoAsset.asset defaultRepresentation] url] absoluteString]]){
  239. selectAssetsCurrentPage = i;
  240. break;
  241. }
  242. }
  243. if (
  244. (self.selectAssets.count > selectAssetsCurrentPage)
  245. &&
  246. (selectAssetsCurrentPage >= 0)
  247. ){
  248. if (deleteAssets){
  249. [self.selectAssets removeObjectAtIndex:selectAssetsCurrentPage];
  250. }
  251. [self.collectionView.selectsIndexPath removeObject:@(selectAssetsCurrentPage)];
  252. [self.toolBarThumbCollectionView reloadData];
  253. self.makeView.text = [NSString stringWithFormat:@"%ld",(long)self.selectAssets.count+self.cameraCount];
  254. }
  255. // 刷新下最小的页数
  256. self.minCount = self.selectAssets.count + (_privateTempMinCount - self.selectAssets.count);
  257. }
  258. }
  259. #pragma mark -
  260. #pragma mark - UICollectionViewDataSource
  261. - (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  262. return 1;
  263. }
  264. - (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  265. return self.selectAssets.count;
  266. }
  267. - (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  268. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:_identifier forIndexPath:indexPath];
  269. if (self.selectAssets.count > indexPath.item) {
  270. UIImageView *imageView = [[cell.contentView subviews] lastObject];
  271. // 判断真实类型
  272. if (![imageView isKindOfClass:[UIImageView class]]) {
  273. imageView = [[UIImageView alloc] initWithFrame:cell.bounds];
  274. imageView.contentMode = UIViewContentModeScaleAspectFit;
  275. imageView.clipsToBounds = YES;
  276. [cell.contentView addSubview:imageView];
  277. }
  278. imageView.tag = indexPath.item;
  279. imageView.image = [self.selectAssets[indexPath.item] originImage];
  280. }
  281. return cell;
  282. }
  283. #pragma mark -
  284. #pragma makr UICollectionViewDelegate
  285. - (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  286. UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  287. DKPhotoPickerBrowserViewController *browserVc = [[DKPhotoPickerBrowserViewController alloc] init];
  288. browserVc.toView = [cell.contentView.subviews lastObject];
  289. browserVc.currentIndexPath = [NSIndexPath indexPathForItem:indexPath.item inSection:0];
  290. browserVc.delegate = self;
  291. browserVc.dataSource = self;
  292. [self presentViewController:browserVc animated:NO completion:nil];
  293. }
  294. - (NSInteger)photoBrowser:(DKPhotoPickerBrowserViewController *)photoBrowser numberOfItemsInSection:(NSUInteger)section{
  295. return self.selectAssets.count;
  296. }
  297. - (DKPhotoPickerBrowserPhoto *)photoBrowser:(DKPhotoPickerBrowserViewController *)pickerBrowser photoAtIndexPath:(NSIndexPath *)indexPath{
  298. DKPhotoPickerBrowserPhoto *photo = [[DKPhotoPickerBrowserPhoto alloc] init];
  299. photo.asset = self.selectAssets[indexPath.row];
  300. return photo;
  301. }
  302. - (void)photoBrowser:(DKPhotoPickerBrowserViewController *)photoBrowser removePhotoAtIndexPath:(NSIndexPath *)indexPath{
  303. // 删除选中的照片
  304. ALAsset *asset = self.selectAssets[indexPath.row];
  305. NSInteger currentPage = 0;
  306. for (NSInteger i = 0; i < self.collectionView.dataArray.count; i++) {
  307. ALAsset *photoAsset = self.collectionView.dataArray[i];
  308. if([[[[asset defaultRepresentation] url] absoluteString] isEqualToString:[[[photoAsset defaultRepresentation] url] absoluteString]]){
  309. currentPage = i;
  310. break;
  311. }
  312. }
  313. [self.selectAssets removeObjectAtIndex:indexPath.row];
  314. [self.collectionView.selectsIndexPath removeObject:@(currentPage)];
  315. [self.toolBarThumbCollectionView reloadData];
  316. [self.collectionView reloadData];
  317. self.makeView.text = [NSString stringWithFormat:@"%ld",(unsigned long)self.selectAssets.count];
  318. }
  319. #pragma mark -<Navigation Actions>
  320. #pragma mark -开启异步通知
  321. - (void) back{
  322. [self dismissViewControllerAnimated:YES completion:nil];
  323. }
  324. - (void) done{
  325. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  326. [[NSNotificationCenter defaultCenter] postNotificationName:PICKER_TAKE_DONE object:nil userInfo:@{@"selectAssets":self.selectAssets}];
  327. });
  328. [self dismissViewControllerAnimated:YES completion:nil];
  329. }
  330. - (void)dealloc{
  331. // 赋值给上一个控制器
  332. self.groupVc.selectAsstes = self.selectAssets;
  333. }
  334. @end