// // DKPhotoPickerAssetsViewController.m // DKAssetsPickerDemo // // Copyright (c) 2014年 com.zixue101.www. All rights reserved. // #import #import "DKPhoto.h" #import "DKPhotoPickerCollectionView.h" #import "DKPhotoPickerGroup.h" #import "DKPhotoPickerCollectionViewCell.h" #import "DKPhotoPickerFooterCollectionReusableView.h" #import "Util.h" static CGFloat CELL_ROW = 4; static CGFloat CELL_MARGIN = 2; static CGFloat CELL_LINE_MARGIN = 2; static CGFloat TOOLBAR_HEIGHT = 44; static NSString *const _cellIdentifier = @"cell"; static NSString *const _footerIdentifier = @"FooterView"; static NSString *const _identifier = @"toolBarThumbCollectionViewCell"; @interface DKPhotoPickerAssetsViewController () // View // 相片View @property (nonatomic , strong) DKPhotoPickerCollectionView *collectionView; // 底部CollectionView @property (nonatomic , weak) UICollectionView *toolBarThumbCollectionView; // 标记View @property (nonatomic , weak) UILabel *makeView; @property (nonatomic , strong) UIButton *doneBtn; @property (nonatomic , weak) UIToolbar *toolBar; @property (assign,nonatomic) NSUInteger privateTempMinCount; // Datas // 数据源 @property (nonatomic , strong) NSMutableArray *assets; // 记录选中的assets @property (nonatomic , strong) NSMutableArray *selectAssets; @end @implementation DKPhotoPickerAssetsViewController #pragma mark - getter #pragma mark Get Data - (NSMutableArray *)selectAssets{ if (!_selectAssets) { _selectAssets = [NSMutableArray array]; } return _selectAssets; } #pragma mark Get View - (UIButton *)doneBtn{ if (!_doneBtn) { UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [rightBtn setTitleColor:[UIColor colorWithRed:0/255.0 green:91/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateNormal]; [rightBtn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled]; rightBtn.enabled = YES; rightBtn.titleLabel.font = [UIFont systemFontOfSize:17]; rightBtn.frame = CGRectMake(0, 0, 45, 45); [rightBtn setTitle:@"完成" forState:UIControlStateNormal]; [rightBtn addTarget:self action:@selector(done) forControlEvents:UIControlEventTouchUpInside]; [rightBtn addSubview:self.makeView]; self.doneBtn = rightBtn; } return _doneBtn; } - (UICollectionView *)toolBarThumbCollectionView{ if (!_toolBarThumbCollectionView) { UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; flowLayout.itemSize = CGSizeMake(40, 40); flowLayout.minimumInteritemSpacing = 0; flowLayout.minimumLineSpacing = 5; flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; // CGRectMake(0, 22, 300, 44) UICollectionView *toolBarThumbCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 0, self.view.width - 100, 44) collectionViewLayout:flowLayout]; toolBarThumbCollectionView.backgroundColor = [UIColor clearColor]; toolBarThumbCollectionView.dataSource = self; toolBarThumbCollectionView.delegate = self; [toolBarThumbCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:_identifier]; self.toolBarThumbCollectionView = toolBarThumbCollectionView; [self.toolBar addSubview:toolBarThumbCollectionView]; } return _toolBarThumbCollectionView; } - (void)setSelectPickerAssets:(NSArray *)selectPickerAssets{ NSSet *set = [NSSet setWithArray:selectPickerAssets]; _selectPickerAssets = [set allObjects]; if (!self.assets) { self.assets = [NSMutableArray arrayWithArray:selectPickerAssets]; }else{ [self.assets addObjectsFromArray:selectPickerAssets]; } for (DKPhotoAssets *assets in selectPickerAssets) { if ([assets isKindOfClass:[DKPhotoAssets class]]) { [self.selectAssets addObject:assets]; } } self.collectionView.lastDataArray = nil; self.collectionView.isRecoderSelectPicker = YES; self.collectionView.selectAsstes = self.selectAssets; NSInteger count = self.selectAssets.count; self.makeView.text = [NSString stringWithFormat:@"%ld",(long)(count+self.cameraCount)]; self.makeView.hidden = !(count+self.cameraCount); self.doneBtn.enabled = (count > 0); } #pragma mark collectionView - (DKPhotoPickerCollectionView *)collectionView{ if (!_collectionView) { CGFloat cellW = (self.view.frame.size.width - CELL_MARGIN * CELL_ROW + 1) / CELL_ROW; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.itemSize = CGSizeMake(cellW, cellW); layout.minimumInteritemSpacing = 0; layout.minimumLineSpacing = CELL_LINE_MARGIN; layout.footerReferenceSize = CGSizeMake(self.view.frame.size.width, TOOLBAR_HEIGHT * 2); DKPhotoPickerCollectionView *collectionView = [[DKPhotoPickerCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; // 时间置顶 collectionView.status = DKPickerCollectionViewShowOrderStatusTimeDesc; collectionView.translatesAutoresizingMaskIntoConstraints = NO; [collectionView registerClass:[DKPhotoPickerCollectionViewCell class] forCellWithReuseIdentifier:_cellIdentifier]; // 底部的View [collectionView registerClass:[DKPhotoPickerFooterCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:_footerIdentifier]; collectionView.contentInset = UIEdgeInsetsMake(5, 0,TOOLBAR_HEIGHT, 0); collectionView.collectionViewDelegate = self; [self.view insertSubview:collectionView belowSubview:self.toolBar]; self.collectionView = collectionView; NSDictionary *views = NSDictionaryOfVariableBindings(collectionView); NSString *widthVfl = @"H:|-0-[collectionView]-0-|"; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:widthVfl options:0 metrics:nil views:views]]; NSString *heightVfl = @"V:|-0-[collectionView]-0-|"; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:heightVfl options:0 metrics:nil views:views]]; } return _collectionView; } #pragma mark makeView 红点标记View - (UILabel *)makeView{ if (!_makeView) { UILabel *makeView = [[UILabel alloc] init]; makeView.textColor = [UIColor whiteColor]; makeView.textAlignment = NSTextAlignmentCenter; makeView.font = [UIFont systemFontOfSize:13]; makeView.frame = CGRectMake(-5, -5, 20, 20); makeView.hidden = YES; makeView.layer.cornerRadius = makeView.frame.size.height / 2.0; makeView.clipsToBounds = YES; makeView.backgroundColor = [UIColor redColor]; [self.view addSubview:makeView]; self.makeView = makeView; } return _makeView; } - (void)viewDidLoad { [super viewDidLoad]; self.view.bounds = [UIScreen mainScreen].bounds; self.view.backgroundColor = [UIColor whiteColor]; // 初始化按钮 [self setupButtons]; // 初始化底部ToorBar [self setupToorBar]; } #pragma mark - setter #pragma mark 初始化按钮 - (void) setupButtons{ self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(back)]; } #pragma mark 初始化所有的组 - (void) setupAssets{ if (!self.assets) { self.assets = [NSMutableArray array]; } __block NSMutableArray *assetsM = [NSMutableArray array]; __weak typeof(self) weakSelf = self; [[DKPhotoPickerDatas defaultPicker] getGroupPhotosWithGroup:self.assetsGroup finished:^(NSArray *assets) { [assets enumerateObjectsUsingBlock:^(ALAsset *asset, NSUInteger idx, BOOL *stop) { DKPhotoAssets *DKAsset = [[DKPhotoAssets alloc] init]; DKAsset.asset = asset; [assetsM addObject:DKAsset]; }]; weakSelf.collectionView.dataArray = assetsM; }]; } #pragma mark -初始化底部ToorBar - (void) setupToorBar{ UIToolbar *toorBar = [[UIToolbar alloc] init]; toorBar.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:toorBar]; self.toolBar = toorBar; NSDictionary *views = NSDictionaryOfVariableBindings(toorBar); NSString *widthVfl = @"H:|-0-[toorBar]-0-|"; NSString *heightVfl = [NSString stringWithFormat:@"%@%f%@",@"V:[toorBar(44)]-",[Util obtainHomeAreaHeight],@"-|"]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:widthVfl options:0 metrics:0 views:views]]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:heightVfl options:0 metrics:0 views:views]]; // 左视图 中间距 右视图 UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:self.toolBarThumbCollectionView]; UIBarButtonItem *fiexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:self.doneBtn]; toorBar.items = @[leftItem,fiexItem,rightItem]; } #pragma mark - setter -(void)setMinCount:(NSInteger)minCount{ _minCount = minCount; if (!_privateTempMinCount) { _privateTempMinCount = minCount; } if (self.selectAssets.count == minCount){ minCount = 0; }else if (self.selectPickerAssets.count - self.selectAssets.count > 0) { minCount = _privateTempMinCount; } self.collectionView.minCount = minCount; } -(void)setCameraCount:(NSInteger)cameraCount{ _cameraCount=cameraCount; self.collectionView.cameraCount = cameraCount; } - (void)setAssetsGroup:(DKPhotoPickerGroup *)assetsGroup{ if (!assetsGroup.groupName.length) return ; _assetsGroup = assetsGroup; self.title = assetsGroup.groupName; // 获取Assets [self setupAssets]; } - (void) pickerCollectionViewDidSelected:(DKPhotoPickerCollectionView *) pickerCollectionView deleteAsset:(DKPhotoAssets *)deleteAssets{ if (self.selectPickerAssets.count == 0){ self.selectAssets = [NSMutableArray arrayWithArray:pickerCollectionView.selectAsstes]; }else if (deleteAssets == nil){ [self.selectAssets addObject:[pickerCollectionView.selectAsstes lastObject]]; } // self.selectAssets = [NSMutableArray arrayWithArray:[[NSSet setWithArray:self.selectAssets] allObjects]]; NSInteger count = self.selectAssets.count; self.makeView.hidden = !count; self.makeView.text = [NSString stringWithFormat:@"%ld",(long)(count+self.cameraCount)]; self.doneBtn.enabled = (count > 0); [self.toolBarThumbCollectionView reloadData]; if (self.selectPickerAssets.count || deleteAssets) { DKPhotoAssets *asset = [pickerCollectionView.lastDataArray lastObject]; if (deleteAssets){ asset = deleteAssets; } NSInteger selectAssetsCurrentPage = -1; for (NSInteger i = 0; i < self.selectAssets.count; i++) { DKPhotoAssets *photoAsset = self.selectAssets[i]; if([[[[asset.asset defaultRepresentation] url] absoluteString] isEqualToString:[[[photoAsset.asset defaultRepresentation] url] absoluteString]]){ selectAssetsCurrentPage = i; break; } } if ( (self.selectAssets.count > selectAssetsCurrentPage) && (selectAssetsCurrentPage >= 0) ){ if (deleteAssets){ [self.selectAssets removeObjectAtIndex:selectAssetsCurrentPage]; } [self.collectionView.selectsIndexPath removeObject:@(selectAssetsCurrentPage)]; [self.toolBarThumbCollectionView reloadData]; self.makeView.text = [NSString stringWithFormat:@"%ld",(long)self.selectAssets.count+self.cameraCount]; } // 刷新下最小的页数 self.minCount = self.selectAssets.count + (_privateTempMinCount - self.selectAssets.count); } } #pragma mark - #pragma mark - UICollectionViewDataSource - (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 1; } - (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return self.selectAssets.count; } - (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:_identifier forIndexPath:indexPath]; if (self.selectAssets.count > indexPath.item) { UIImageView *imageView = [[cell.contentView subviews] lastObject]; // 判断真实类型 if (![imageView isKindOfClass:[UIImageView class]]) { imageView = [[UIImageView alloc] initWithFrame:cell.bounds]; imageView.contentMode = UIViewContentModeScaleAspectFit; imageView.clipsToBounds = YES; [cell.contentView addSubview:imageView]; } imageView.tag = indexPath.item; imageView.image = [self.selectAssets[indexPath.item] originImage]; } return cell; } #pragma mark - #pragma makr UICollectionViewDelegate - (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath]; DKPhotoPickerBrowserViewController *browserVc = [[DKPhotoPickerBrowserViewController alloc] init]; browserVc.toView = [cell.contentView.subviews lastObject]; browserVc.currentIndexPath = [NSIndexPath indexPathForItem:indexPath.item inSection:0]; browserVc.delegate = self; browserVc.dataSource = self; [self presentViewController:browserVc animated:NO completion:nil]; } - (NSInteger)photoBrowser:(DKPhotoPickerBrowserViewController *)photoBrowser numberOfItemsInSection:(NSUInteger)section{ return self.selectAssets.count; } - (DKPhotoPickerBrowserPhoto *)photoBrowser:(DKPhotoPickerBrowserViewController *)pickerBrowser photoAtIndexPath:(NSIndexPath *)indexPath{ DKPhotoPickerBrowserPhoto *photo = [[DKPhotoPickerBrowserPhoto alloc] init]; photo.asset = self.selectAssets[indexPath.row]; return photo; } - (void)photoBrowser:(DKPhotoPickerBrowserViewController *)photoBrowser removePhotoAtIndexPath:(NSIndexPath *)indexPath{ // 删除选中的照片 ALAsset *asset = self.selectAssets[indexPath.row]; NSInteger currentPage = 0; for (NSInteger i = 0; i < self.collectionView.dataArray.count; i++) { ALAsset *photoAsset = self.collectionView.dataArray[i]; if([[[[asset defaultRepresentation] url] absoluteString] isEqualToString:[[[photoAsset defaultRepresentation] url] absoluteString]]){ currentPage = i; break; } } [self.selectAssets removeObjectAtIndex:indexPath.row]; [self.collectionView.selectsIndexPath removeObject:@(currentPage)]; [self.toolBarThumbCollectionView reloadData]; [self.collectionView reloadData]; self.makeView.text = [NSString stringWithFormat:@"%ld",(unsigned long)self.selectAssets.count]; } #pragma mark - #pragma mark -开启异步通知 - (void) back{ [self dismissViewControllerAnimated:YES completion:nil]; } - (void) done{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [[NSNotificationCenter defaultCenter] postNotificationName:PICKER_TAKE_DONE object:nil userInfo:@{@"selectAssets":self.selectAssets}]; }); [self dismissViewControllerAnimated:YES completion:nil]; } - (void)dealloc{ // 赋值给上一个控制器 self.groupVc.selectAsstes = self.selectAssets; } @end