123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- //
- // Collection_Image.m
- // 乐销
- //
- // Created by 隋林栋 on 2017/1/9.
- // Copyright © 2017年 ping. All rights reserved.
- //
- #import "Collection_Image.h"
- #import "CollectionImageCell.h"//cell
- #import "ImageDetailBigView.h"//detail
- @interface Collection_Image()<ImagePickerVCDelegate>
- @property (nonatomic, strong) XG_AssetModel *placeholderModel;
- @end
- @implementation Collection_Image
- #pragma mark 懒加载
- -(NSMutableArray<XG_AssetModel *> *)assets{
- if (!_assets) {
- _assets = @[self.placeholderModel].mutableCopy;
- }
- return _assets;
- }
- -(XG_AssetModel *)placeholderModel{
- if (!_placeholderModel) {
- _placeholderModel = [[XG_AssetModel alloc]init];
- _placeholderModel.isPlaceholder = YES;
- }
- return _placeholderModel;
- }
- - (NSMutableArray *)aryPhoto{
- if (!_aryPhoto) {
- _aryPhoto = [NSMutableArray new];
- }
- return _aryPhoto;
- }
- - (void)openAlbum{
- WEAKSELF
- [[XG_AssetPickerManager manager] handleAuthorizationWithCompletion:^(XG_AuthorizationStatus aStatus) {
- if (aStatus == XG_AuthorizationStatusAuthorized) {
- [weakSelf showAssetPickerController];
- }else{
- [weakSelf showAlert];
- }
- }];
- }
- - (void)showAssetPickerController{
- XG_AssetPickerOptions *options = [[XG_AssetPickerOptions alloc]init];
- options.maxAssetsCount = 9;
- options.videoPickable = false;
- NSMutableArray<XG_AssetModel *> *array = self.assets;
- [array removeLastObject];//去除占位model
- options.pickedAssetModels = array;
- XG_AssetPickerController *photoPickerVc = [[XG_AssetPickerController alloc] initWithOptions:options delegate:self];
- [GB_Nav pushViewController:photoPickerVc animated:true];
- }
- - (void)showAlert{
- ModelBtn * modelDismiss = [ModelBtn modelWithTitle:@"取消" imageName:nil highImageName:nil tag:TAG_LINE color:[UIColor redColor]];
- ModelBtn * modelConfirm = [ModelBtn modelWithTitle:@"去设置" imageName:nil highImageName:nil tag:TAG_LINE color:COLOR_BLUE];
- modelConfirm.blockClick = ^(void){
- //去设置
- NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
- if ([[UIApplication sharedApplication] canOpenURL:url]) {
- [[UIApplication sharedApplication] openURL:url];
- }
- };
- [BaseAlertView initWithTitle:@"" content:@"未开启相册权限,是否去设置中开启?" aryBtnModels:@[modelDismiss,modelConfirm] viewShow:[UIApplication sharedApplication].keyWindow];
- }
- - (void)onDeleteBtnClick:(UIButton *)sender{
- /*
- performBatchUpdates并不会调用代理方法collectionView: cellForItemAtIndexPath,
- 如果用删除按钮的tag来标识则tag不会更新,所以此处没有用tag
- */
- CollectionImageCell *cell = (CollectionImageCell *)sender.superview.superview;
- NSIndexPath *indexpath = [self.collectionView indexPathForCell:cell];
- [self.collectionView performBatchUpdates:^{
- [self.collectionView deleteItemsAtIndexPaths:@[indexpath]];
- [self.assets removeObjectAtIndex:indexpath.item];
- NSMutableArray *arr = [NSMutableArray array];
- for (XG_AssetModel *model in self.assets) {
- [arr addObject:@(model.isPlaceholder)];
- }
- if (self.assets.count == 8 && (![self.assets containsObject:self.placeholderModel]&&![arr containsObject:@(YES)])) {
- [self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:8 inSection:0]]];
- [self.assets addObject:self.placeholderModel];
-
- }
- } completion:^(BOOL finished) {
-
- }];
- NSMutableArray *newAssets = self.assets.mutableCopy;
- NSMutableArray *arr = [NSMutableArray array];
- for (int i =0; i<self.assets.count; i++) {
- XG_AssetModel *model =self.assets[i];
- model.number = 0;
- for (int j=0; j<newAssets.count; j++) {
- XG_AssetModel *pam = newAssets[j];
- if ([model.asset.localIdentifier isEqualToString:pam.asset.localIdentifier]) {
- model.picked = YES;
- model.number = j+1;
- [arr addObject:model];
- }
- }
- }
- if (self.blockDelComplete) {
- self.blockDelComplete(self.assets);
- }
- }
- #pragma mark - UICollectionViewDataSource
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
- return self.assets.count;
- }
- - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
- CollectionImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([CollectionImageCell class]) forIndexPath:indexPath];
- cell.imgName=self.imgName;
- cell.model = self.assets[indexPath.item];
- // [cell resetWithModel:self.assets[indexPath.item]];
- [cell.btnDelete addTarget:self action:@selector(onDeleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
- WEAKSELF
- [self.aryPhoto removeAllObjects];
- cell.blockModel = ^(UIImage *photo) {
- [weakSelf.aryPhoto addObject:photo];
- };
- return cell;
- }
- #pragma mark - UICollectionViewDelegate
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
- if (!isAry(self.assets)) {
- [self.assets addObject:self.placeholderModel];
- }
- XG_AssetModel *model = self.assets[indexPath.item];
- if (model.isPlaceholder) {
- [self openAlbum];
- }
- }
- #pragma mark - XG_AssetPickerControllerDelegate
- - (void)assetPickerController:(XG_AssetPickerController *)picker didFinishPickingAssets:(NSArray<XG_AssetModel *> *)assets{
- NSMutableArray *newAssets = assets.mutableCopy;
- if (newAssets.count < 9 ) {
- [newAssets addObject:self.placeholderModel];
- }
- [self.assets removeAllObjects];
- [self.assets addObjectsFromArray: newAssets];
- NSMutableArray *arr = [NSMutableArray arrayWithCapacity:9];
- for (int j = 0; j < 9; j++) {
- [arr addObject:[NSNull null]];
- }
- NSMutableArray *arru = [NSMutableArray array];
- dispatch_queue_t serialQueue=dispatch_queue_create("串行", DISPATCH_QUEUE_SERIAL);
- dispatch_sync(serialQueue, ^{
- for (int i =0; i<assets.count; i++) {
- XG_AssetModel *model =assets[i];
- if (model.url) {
- [arru addObject:model];
- }
- if (model.asset) {
- [[XG_AssetPickerManager manager] getPhotoWithAsset1:model.asset completion:^(UIImage *photo, NSDictionary *info) {
- if (isAry(arru)) {
- [arr replaceObjectAtIndex:model.number-arru.count-1 withObject:photo];
- }else{
- [arr replaceObjectAtIndex:model.number-1 withObject:photo];
- }
- }];
-
- }
- }
- });
- if (self.blockUpComplete) {
- self.blockUpComplete(arr);
- }
- // [self resetView];
- [self.collectionView reloadData];
- }
- - (UICollectionView *)collectionView{
- if (_collectionView == nil) {
- // 1.流水布局
- UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
-
- layout.minimumInteritemSpacing = 1;
- layout.minimumLineSpacing = 1;
- layout.sectionInset = UIEdgeInsetsMake(1, W(7), 0, W(5));
- CGSize size = [self fetchCellSize];
- layout.itemSize = size;
- layout.scrollDirection = self.scrollDirection;
- // 6.创建UICollectionView
- _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.height) collectionViewLayout:layout];
- // 7.设置collectionView的背景色
- _collectionView.backgroundColor = [UIColor whiteColor];
- // 8.设置代理
- _collectionView.collectionViewLayout = layout;
- _collectionView.delegate = self;
- _collectionView.dataSource = self;
- _collectionView.scrollEnabled = YES;
- _collectionView.showsVerticalScrollIndicator = false;
- _collectionView.showsHorizontalScrollIndicator = false;
- [_collectionView registerClass:NSClassFromString(self.collectionCellName) forCellWithReuseIdentifier:self.collectionCellName];
- }
- return _collectionView;
- }
- #pragma mark 初始化
- - (instancetype)init{
- return [self initWithConfig:true];
- }
- - (instancetype)initWithConfig:(BOOL)config{
- self = [super init];
- if (self) {
- //default
- self.scrollDirection = UICollectionViewScrollDirectionVertical;
- self.isShowTitleBottom = false;
- self.isEditing = true;
- self.backgroundColor = [UIColor clearColor];
- self.widthHeight = XY(SCREEN_WIDTH, 0);
- self.collectionCellName = @"CollectionImageCell";
- //config
- if (config) {
- [self configView];
- }
- }
- return self;
- }
- - (void)configView{
- [self resetView];
- [self addSubview:self.collectionView];
- self.collectionView.contentInset = UIEdgeInsetsMake(W(1), W(10), 0, self.scrollDirection == UICollectionViewScrollDirectionVertical?W(20):0);
- }
- -(void)resetView{
- CGSize size = [self fetchCellSize];
- NSInteger count = self.assets.count;
- self.height= count%3 == 0 ? count/3 * size.height + count/3 :count/3 * size.height + count/3 + size.height;
- self.collectionView.height = self.height;
- [self.collectionView reloadData];
-
- // self.height = (sizeCell.height+W(15))*self.assets.count;
- }
- #pragma mark fetch cell size
- - (CGSize)fetchCellSize{
- return self.isShowTitleBottom?[NSClassFromString(self.collectionCellName) fetchHeightWithTitle]:[NSClassFromString(self.collectionCellName) fetchHeight];
- }
- @end
|