123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799 |
- //
- // RITLPhotosItemsCollectionViewController.m
- // RITLPhotoDemo
- //
- // Created by YueWen on 2018/3/7.
- // Copyright © 2018年 YueWen. All rights reserved.
- //
- #import "RITLPhotosCollectionViewController.h"
- #import "RITLPhotosPreviewController.h"
- #import "RITLPhotosHorBrowseViewController.h"
- #import "RITLPhotosCell.h"
- #import "RITLPhotosBottomView.h"
- #import "RITLPhotosBrowseAllDataSource.h"
- #import "RITLPhotosBrowseDataSource.h"
- #import "RITLPhotosConfiguration.h"
- #import "PHAsset+RITLPhotos.h"
- #import "NSString+RITLPhotos.h"
- #import "PHPhotoLibrary+RITLPhotoStore.h"
- #import "UICollectionView+RITLIndexPathsForElements.h"
- #import "UICollectionViewCell+RITLPhotosAsset.h"
- #import "RITLKit.h"
- #import "UIView+RITLFrameChanged.h"
- #import "Masonry.h"
- #import <Photos/Photos.h>
- #import "UIImage+Tint.h"
- //Data
- #import "RITLPhotosMaker.h"
- #import "RITLPhotosDataManager.h"
- #import "RITLPhotosConfiguration.h"
- typedef NSString RITLDifferencesKey;
- static RITLDifferencesKey *const RITLDifferencesKeyAdded = @"RITLDifferencesKeyAdded";
- static RITLDifferencesKey *const RITLDifferencesKeyRemoved = @"RITLDifferencesKeyRemoved";
- @interface UICollectionView (RITLPhotosCollectionViewController)
- - (NSArray <NSIndexPath *>*)indexPathsForElementsInRect:(CGRect)rect;
- @end
- @interface RITLPhotosCollectionViewController ()<UICollectionViewDataSource,
- UICollectionViewDelegateFlowLayout,
- UIViewControllerPreviewingDelegate,
- UICollectionViewDataSourcePrefetching,
- RITLPhotosCellActionTarget>
- // Library
- @property (nonatomic, strong) PHPhotoLibrary *photoLibrary;
- // Datamanager
- @property (nonatomic, strong) RITLPhotosDataManager *dataManager;
- // data
- @property (nonatomic, strong) UICollectionView *collectionView;
- @property (nonatomic, strong) PHAssetCollection *assetCollection;
- @property (nonatomic, strong) PHFetchResult <PHAsset *> *assets;
- @property (nonatomic, strong) PHCachingImageManager* imageManager;
- @property (nonatomic, assign) CGRect previousPreheatRect;
- // view
- @property (nonatomic, strong) RITLPhotosBottomView *bottomView;
- @property (nonatomic, strong) UILabel *tintLable; // 无权限提醒
- @property (nonatomic, assign) BOOL isEdit;
- @property (nonatomic, strong) PHAsset *oldAsset;
- @property (nonatomic, assign) BOOL isScroll;
- @end
- @implementation RITLPhotosCollectionViewController
- static NSString *const reuseIdentifier = @"photo";
- + (RITLPhotosCollectionViewController *)photosCollectionController
- {
- return RITLPhotosCollectionViewController.new;
- }
- - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
-
- self.previousPreheatRect = CGRectZero;
- self.dataManager = [RITLPhotosDataManager sharedInstance];
- }
-
- return self;
- }
- - (void)resetCachedAssets
- {
- [self.imageManager stopCachingImagesForAllAssets];
- self.previousPreheatRect = CGRectZero;
- }
- - (PHAssetCollection *)assetCollection
- {
- if (!_assetCollection) {
-
- _assetCollection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil].firstObject;
- }
-
- return _assetCollection;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- self.view.backgroundColor = UIColor.whiteColor;
- //设置navigationController 背景颜色
- UIImage *iv = [[UIImage alloc] init];
- if (THESIMPLESTYLE) {
- iv = [[UIImage imageNamed:@"navBarBackground"] imageWithTintColor:[UIColor whiteColor]];
- }else {
- iv = [g_theme themeTintImage:@"navBarBackground"];
- }
- [self.navigationController.navigationBar setBackgroundImage:iv forBarMetrics:UIBarMetricsDefault];
- [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
- //进行KVO观察
- [self.dataManager addObserver:self forKeyPath:@"count" options:NSKeyValueObservingOptionNew context:nil];
- [self.dataManager addObserver:self forKeyPath:@"hightQuality" options:NSKeyValueObservingOptionNew context:nil];
-
-
- // NavigationItem
-
- UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 32, 18)];
- [btn1 setTitle:Localized(@"JX_Cencal") forState:UIControlStateNormal];
- [btn1 setTitleColor:HEXCOLOR(0x333333) forState:UIControlStateNormal];
- [btn1.titleLabel setFont:SYSFONT(15)];
- [btn1 addTarget:self action:@selector(actionQuit) forControlEvents:UIControlEventTouchUpInside];
- UIBarButtonItem *barBtn1 = [[UIBarButtonItem alloc] initWithCustomView:btn1];
- self.navigationItem.rightBarButtonItem = barBtn1;
-
- UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 18, 18)];
- [btn setBackgroundImage:[UIImage imageNamed:@"title_back_black"] forState:UIControlStateNormal];
- [btn addTarget:self action:@selector(actionQuit) forControlEvents:UIControlEventTouchUpInside];
- UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
- self.navigationItem.leftBarButtonItem = barBtn;
- // Register cell classes
- [self.collectionView registerClass:[RITLPhotosCell class] forCellWithReuseIdentifier:reuseIdentifier];
-
- self.bottomView = RITLPhotosBottomView.new;
- self.bottomView.previewButton.enabled = false;
- self.bottomView.sendButton.enabled = false;
-
- [self.bottomView.previewButton addTarget:self
- action:@selector(pushPreviewViewController)
- forControlEvents:UIControlEventTouchUpInside];
-
- [self.bottomView.fullImageButton addTarget:self
- action:@selector(hightQualityShouldChanged:)
- forControlEvents:UIControlEventTouchUpInside];
-
- [self.bottomView.sendButton addTarget:self
- action:@selector(pushPhotosMaker)
- forControlEvents:UIControlEventTouchUpInside];
-
- self.bottomView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.8];
-
- // Do any additional setup after loading the view.
- [self.view addSubview:self.collectionView];
- [self.view addSubview:self.bottomView];
- [self.view addSubview:self.tintLable];
- // Layout
- [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
-
- make.bottom.left.right.offset(0);
- make.height.mas_equalTo(RITL_DefaultTabBarHeight - 3);
- }];
-
- [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
-
- make.left.top.right.offset(0);
- make.bottom.equalTo(self.bottomView.mas_top).offset(0);
- }];
-
- [self.tintLable mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.offset(JX_SCREEN_TOP+20);
- make.left.offset(30);
- make.right.offset(-30);
- make.height.mas_equalTo(60);
- }];
- //加载数据
- if (self.localIdentifier) {
- //加载
- self.assetCollection = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[self.localIdentifier] options:nil].firstObject;
- }
-
- //进行权限检测
- [PHPhotoLibrary authorizationStatusAllow:^{
-
- self.imageManager = [PHCachingImageManager new];
- self.photoLibrary = PHPhotoLibrary.sharedPhotoLibrary;
-
- [self resetCachedAssets];
-
- self.assets = [PHAsset fetchAssetsInAssetCollection:self.assetCollection options:nil];
-
- //reload
- self.collectionView.hidden = true;
- [self.collectionView reloadData];
-
- // 拥有照片访问权限
- self.tintLable.hidden = YES;
- self.bottomView.hidden = NO;
-
- //设置itemTitle
- // self.navigationItem.title = self.assetCollection.localizedTitle;
- self.navigationItem.title = Localized(@"JX_Photo");
- [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
-
- //计算行数,并滑动到最后一行
- self.collectionView.hidden = false;
-
- [self collectionViewScrollToBottomAnimatedNoneHandler:^NSInteger(NSInteger row) {
-
- return row;
- }];
-
- // 滚动到底部
- [self.collectionView setContentOffset:CGPointMake(0, self.collectionView.contentSize.height - self.collectionView.frame.size.height) animated:YES];
- } denied:^{
- // 没有照片访问权限
- self.tintLable.hidden = NO;
- self.bottomView.hidden = YES;
- }];
-
- [self changedBottomViewStatus];
- }
- - (void)actionQuit {
- [self dismissPhotoControllers];
- }
- - (void)dealloc
- {
- if (self.isViewLoaded) {
- [self.dataManager removeObserver:self forKeyPath:@"count"];
- [self.dataManager removeObserver:self forKeyPath:@"hightQuality"];
- }
- [self.dataManager removeAllPHAssets];
- NSLog(@"[%@] is dealloc",NSStringFromClass(self.class));
- }
- - (void)collectionViewScrollToBottomAnimatedNoneHandler:(NSInteger(^)(NSInteger row))handler
- {
- //获得所有的数据个数
- NSInteger itemCount = self.assets.count;
-
- if (itemCount < 4) { return; }
-
- //获得行数
- NSInteger row = itemCount % 4 == 0 ? itemCount / 4 : itemCount / 4 + 1;
-
- if (handler) { row = handler(row); }
-
- //item
- CGFloat itemHeight = (RITL_SCREEN_WIDTH - 3.0f * 3) / 4;
-
- //进行高度换算
- CGFloat height = row * itemHeight;
- height += 3.0 * (row - 1);
-
- //扩展contentSize
- self.collectionView.ritl_contentSizeHeight = height;
-
- //底部bottom
- CGFloat bottomHeight = RITL_DefaultTabBarHeight;
-
- //可以显示的区域
- CGFloat showSapce = RITL_SCREEN_HEIGHT - RITL_DefaultNaviBarHeight - bottomHeight;
-
- //进行单位换算
- self.collectionView.ritl_contentOffSetY = MIN(MAX(0,height - showSapce),-1 * RITL_DefaultNaviBarHeight);
- }
- - (void)viewDidAppear:(BOOL)animated
- {
- [super viewDidAppear:animated];
- // if (self.isEdit) {
- // self.imageManager = [PHCachingImageManager new];
- // self.photoLibrary = PHPhotoLibrary.sharedPhotoLibrary;
- //
- // [self resetCachedAssets];
- //
- // self.assets = [PHAsset fetchAssetsInAssetCollection:self.assetCollection options:nil];
- // [self.collectionView reloadData];
- // if (self.collectionView.contentOffset.y < self.collectionView.contentSize.height) {
- // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- // // 滚动到底部
- // [self.collectionView setContentOffset:CGPointMake(0, self.collectionView.contentSize.height - self.collectionView.frame.size.height) animated:NO];
- // });
- // }
- // }
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #pragma mark - *************** cache ***************
- - (void)updateCachedAssets
- {
- if (!self.isViewLoaded || self.view.window == nil) { return; }
-
- //没有权限,关闭
- if (PHPhotoLibrary.authorizationStatus != PHAuthorizationStatusAuthorized) { return; }
-
- //可视化
- CGRect visibleRect = CGRectMake(self.collectionView.ritl_contentOffSetX, self.collectionView.ritl_contentOffSetY, self.collectionView.ritl_width, self.collectionView.ritl_height);
-
- //进行拓展
- CGRect preheatRect = CGRectInset(visibleRect, 0, -0.5 * visibleRect.size.height);
-
- //只有可视化的区域与之前的区域有显著的区域变化才需要更新
- CGFloat delta = ABS(CGRectGetMidY(preheatRect) - CGRectGetMidY(self.previousPreheatRect));
- if (delta <= self.view.ritl_height / 3.0) { return; }
-
- //获得比较后需要进行预加载以及需要停止缓存的区域
- NSDictionary *differences = [self differencesBetweenRects:self.previousPreheatRect new:preheatRect];
- NSArray <NSValue *> *addedRects = differences[RITLDifferencesKeyAdded];
- NSArray <NSValue *> *removedRects = differences[RITLDifferencesKeyRemoved];
-
- ///进行提前缓存的资源
- NSArray <PHAsset *> *addedAssets = [[[addedRects ritl_map:^id _Nonnull(NSValue * _Nonnull rectValue) {
- return [self.collectionView indexPathsForElementsInRect:rectValue.CGRectValue];
-
- }] ritl_reduce:@[] reduceHandler:^NSArray * _Nonnull(NSArray * _Nonnull result, NSArray <NSIndexPath *>*_Nonnull items) {
- return [result arrayByAddingObjectsFromArray:items];
-
- }] ritl_map:^id _Nonnull(NSIndexPath *_Nonnull index) {
- return [self.assets objectAtIndex:index.item];
-
- }];
-
- ///提前停止缓存的资源
- NSArray <PHAsset *> *removedAssets = [[[removedRects ritl_map:^id _Nonnull(NSValue * _Nonnull rectValue) {
- return [self.collectionView indexPathsForElementsInRect:rectValue.CGRectValue];
-
- }] ritl_reduce:@[] reduceHandler:^NSArray * _Nonnull(NSArray * _Nonnull result, NSArray <NSIndexPath *>* _Nonnull items) {
- return [result arrayByAddingObjectsFromArray:items];
-
- }] ritl_map:^id _Nonnull(NSIndexPath *_Nonnull index) {
- return [self.assets objectAtIndex:index.item];
- }];
-
- CGSize thimbnailSize = [self collectionView:self.collectionView layout:self.collectionView.collectionViewLayout sizeForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
-
- //更新缓存
- [self.imageManager startCachingImagesForAssets:addedAssets targetSize:thimbnailSize contentMode:PHImageContentModeAspectFill options:nil];
- [self.imageManager stopCachingImagesForAssets:removedAssets targetSize:thimbnailSize contentMode:PHImageContentModeAspectFill options:nil];
-
- //记录当前位置
- self.previousPreheatRect = preheatRect;
- }
- - (NSDictionary <RITLDifferencesKey*,NSArray<NSValue *>*> *)differencesBetweenRects:(CGRect)old new:(CGRect)new
- {
- if (CGRectIntersectsRect(old, new)) {//如果区域交叉
-
- NSMutableArray <NSValue *> * added = [NSMutableArray arrayWithCapacity:10];
- if (CGRectGetMaxY(new) > CGRectGetMaxY(old)) {//表示上拉
- [added addObject:[NSValue valueWithCGRect:CGRectMake(new.origin.x, CGRectGetMaxY(old), new.size.width, CGRectGetMaxY(new) - CGRectGetMaxY(old))]];
- }
-
- if(CGRectGetMinY(old) > CGRectGetMinY(new)){//表示下拉
-
- [added addObject:[NSValue valueWithCGRect:CGRectMake(new.origin.x, CGRectGetMinY(new), new.size.width, CGRectGetMinY(old) - CGRectGetMinY(new))]];
- }
-
- NSMutableArray <NSValue *> * removed = [NSMutableArray arrayWithCapacity:10];
- if (CGRectGetMaxY(new) < CGRectGetMaxY(old)) {//表示下拉
- [removed addObject:[NSValue valueWithCGRect:CGRectMake(new.origin.x, CGRectGetMaxY(new), new.size.width, CGRectGetMaxY(old) - CGRectGetMaxY(new))]];
- }
-
- if (CGRectGetMinY(old) < CGRectGetMinY(new)) {//表示上拉
-
- [removed addObject:[NSValue valueWithCGRect:CGRectMake(new.origin.x, CGRectGetMinY(old), new.size.width, CGRectGetMinY(new) - CGRectGetMinY(old))]];
- }
-
- return @{RITLDifferencesKeyAdded:added,
- RITLDifferencesKeyRemoved:removed};
- }else {
-
- return @{RITLDifferencesKeyAdded:@[[NSValue valueWithCGRect:new]],
- RITLDifferencesKeyRemoved:@[[NSValue valueWithCGRect:old]]};
- }
- }
- #pragma mark - Dismiss
- - (void)dismissPhotoControllers
- {
- if(self.navigationController.presentingViewController){//如果是模态弹出
-
- [self.navigationController dismissViewControllerAnimated:true completion:nil];
-
- }else if(self.navigationController){
-
- [self.navigationController popViewControllerAnimated:true];
- }
- }
- #pragma mark <UICollectionViewDataSource>
- - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
- {
- return 1;
- }
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
- {
- return self.assets ? self.assets.count : 0;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
-
- RITLPhotosCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
-
- //Asset
- PHAsset *asset = [self.assets objectAtIndex:indexPath.item];
-
- //Size
- CGSize size = [self collectionView:collectionView layout:collectionView.collectionViewLayout sizeForItemAtIndexPath:indexPath];
-
- PHImageRequestOptions *options = PHImageRequestOptions.new;
- options.networkAccessAllowed = true;
-
- // Configure the cell
- cell.representedAssetIdentifier = asset.localIdentifier;
- [self.imageManager requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeAspectFill options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
-
- if ([cell.representedAssetIdentifier isEqualToString:asset.localIdentifier] && result) {
-
- cell.actionTarget = self;
- cell.asset = asset;
- cell.indexPath = indexPath;
- cell.imageView.image = result;
- cell.messageView.hidden = (asset.mediaType == PHAssetMediaTypeImage);
- if (self.isEdit) {
-
- if (self.oldAsset) {
- if ([asset.localIdentifier isEqualToString:self.oldAsset.localIdentifier]) {
- BOOL isSelected = [self.dataManager.assetIdentiers containsObject:asset.localIdentifier];
- if (isSelected) {
- // 当编辑图片返回时 如果是选中状态,则取消选中
- [cell.chooseButton sendActionsForControlEvents:UIControlEventTouchUpInside];
- }
- }
- }
- //6CAF1A6D-9377-43A4-8D47-87E1CDFF2750/L0/001
- if (indexPath.row == self.assets.count -1) {
- // 当编辑图片返回时 默认选中,这里是模拟用户点击选择按钮
- [cell.chooseButton sendActionsForControlEvents:UIControlEventTouchUpInside];
- self.isEdit = NO;
- self.isScroll = YES;
- }
- }
- if (@available(iOS 9.1,*)) {//Live图片
-
- cell.liveBadgeImageView.hidden = !(asset.mediaSubtypes == PHAssetMediaSubtypePhotoLive);
- }
-
- if(!RITLPhotosConfiguration.defaultConfiguration.containVideo){//是否允许选择视频-不允许选择视频,去掉选择符
-
- cell.chooseButton.hidden = (asset.mediaType == PHAssetMediaTypeVideo);
- }
- if(!RITLPhotosConfiguration.defaultConfiguration.containImage){//是否允许选择图片-不允许选择图片,去掉选择符
-
- cell.chooseButton.hidden = (asset.mediaType == PHAssetMediaTypeImage);
- }
- BOOL isSelected = [self.dataManager.assetIdentiers containsObject:asset.localIdentifier];
- //进行属性隐藏设置
- cell.indexLabel.hidden = !isSelected;
-
- if (isSelected) {
-
- cell.indexLabel.text = @([self.dataManager.assetIdentiers indexOfObject:asset.localIdentifier] + 1).stringValue;
- }
-
- if (cell.imageView.hidden) { return; }
-
- cell.messageLabel.text = [NSString timeStringWithTimeDuration:asset.duration];
- }
- }];
-
- //注册3D Touch
- if (@available(iOS 9.0,*)) {
- if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable){
-
- [self registerForPreviewingWithDelegate:self sourceView:cell];
- }
- }
- return cell;
- }
- - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
- // if (self.isScroll && indexPath.row == self.assets.count -1) {
- // self.isScroll = NO;
- // if (self.collectionView.contentOffset.y < self.collectionView.contentSize.height) {
- // // 滚动到底部
- // [self.collectionView setContentOffset:CGPointMake(0, self.collectionView.contentSize.height - self.collectionView.frame.size.height) animated:YES];
- // }
- // }
- }
- #pragma mark <UIScrollViewDelegate>
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView
- {
- [self updateCachedAssets];
- }
- #pragma mark <UICollectionViewDelegateFlowLayout>
- - (CGSize)collectionView:(UICollectionView *)collectionView
- layout:(UICollectionViewLayout*)collectionViewLayout
- sizeForItemAtIndexPath:(NSIndexPath *)indexPath
- {
- CGFloat sizeHeight = (MIN(RITL_SCREEN_WIDTH,RITL_SCREEN_HEIGHT) - 3.0f * 3) / 4;
-
- return CGSizeMake(sizeHeight, sizeHeight);
- }
- - (CGFloat)collectionView:(UICollectionView *)collectionView
- layout:(UICollectionViewLayout*)collectionViewLayout
- minimumLineSpacingForSectionAtIndex:(NSInteger)section
- {
- return 3.f;
- }
- - (CGFloat)collectionView:(UICollectionView *)collectionView
- layout:(UICollectionViewLayout *)collectionViewLayout
- minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
- {
- return 3.f;
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
- {
- //获取当前的资源
- PHAsset *asset = [self.assets objectAtIndex:indexPath.item];
- //跳出控制器
- [self pushHorAllBrowseViewControllerWithAsset:asset];
- }
- #pragma mark - <UIViewControllerPreviewingDelegate>
- - (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location NS_AVAILABLE_IOS(9_0)
- {
- //获取当前cell的indexPath
- NSIndexPath * indexPath = [self.collectionView indexPathForCell:(RITLPhotosCell *)previewingContext.sourceView];
-
- NSUInteger item = indexPath.item;
-
- //获得当前的资源
- PHAsset *asset = self.assets[item];
-
- if (asset.mediaType != PHAssetMediaTypeImage)
- {
- return nil;
- }
-
- RITLPhotosPreviewController * viewController = [RITLPhotosPreviewController previewWithShowAsset:asset];
-
- return viewController;
- }
- - (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit NS_AVAILABLE_IOS(9_0)
- {
- //获取当前cell的indexPath
- NSIndexPath * indexPath = [self.collectionView indexPathForCell:(RITLPhotosCell *)previewingContext.sourceView];
-
- //获取当前的资源
- PHAsset *asset = [self.assets objectAtIndex:indexPath.item];
-
- //跳出控制器
- [self pushHorAllBrowseViewControllerWithAsset:asset];
- }
- #pragma mark - Browse All Assets Display
- /// Push 出所有的资源浏览
- - (void)pushHorAllBrowseViewControllerWithAsset:(PHAsset *)asset
- {
- [self.navigationController pushViewController:({
-
- RITLPhotosHorBrowseViewController *browerController = RITLPhotosHorBrowseViewController.new;
- RITLPhotosBrowseAllDataSource *dataSource = RITLPhotosBrowseAllDataSource.new;
- dataSource.collection = self.assetCollection;
- dataSource.asset = asset;
-
- browerController.dataSource = dataSource;
- browerController.backHandler = ^(BOOL isEdit, PHAsset *asset) {
- self.isEdit = isEdit;
- self.oldAsset = asset;
- if (!self.isEdit) {
- [self.collectionView reloadData];
- }
- [self editAfterReloadCollectionView];
- };
-
- browerController;
-
- }) animated:true];
- }
- /// Push出已经选择的资源浏览
- - (void)pushPreviewViewController
- {
- [self.navigationController pushViewController:({
-
- RITLPhotosHorBrowseViewController *browerController = RITLPhotosHorBrowseViewController.new;
-
- RITLPhotosBrowseDataSource *dataSource = RITLPhotosBrowseDataSource.new;
- dataSource.assets = self.dataManager.assets;
-
- browerController.dataSource = dataSource;
- browerController.backHandler = ^(BOOL isEdit, PHAsset *asset) {
- self.isEdit = isEdit;
- self.oldAsset = asset;
- if (!self.isEdit) {
- [self.collectionView reloadData];
- }
- [self editAfterReloadCollectionView];
- };
-
- browerController;
-
- }) animated:true];
- }
- - (void)editAfterReloadCollectionView {
- if (self.isEdit) {
- self.imageManager = [PHCachingImageManager new];
- self.photoLibrary = PHPhotoLibrary.sharedPhotoLibrary;
-
- [self resetCachedAssets];
-
- self.assets = [PHAsset fetchAssetsInAssetCollection:self.assetCollection options:nil];
- [self.collectionView reloadData];
- if (self.collectionView.contentOffset.y < self.collectionView.contentSize.height) {
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- // 滚动到底部
- [self.collectionView setContentOffset:CGPointMake(0, self.collectionView.contentSize.height - self.collectionView.frame.size.height) animated:NO];
- });
- }
- }
- }
- #pragma mark - Send
- - (void)pushPhotosMaker {
- [RITLPhotosMaker.sharedInstance startMakePhotosComplete:^{
-
- [self dismissPhotoControllers];
- }];
- }
- #pragma mark - collectionView
- -(UICollectionView *)collectionView
- {
- if(_collectionView == nil)
- {
- _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewFlowLayout alloc]init]];
-
- //protocol
- _collectionView.delegate = self;
- _collectionView.dataSource = self;
-
- //property
- _collectionView.backgroundColor = [UIColor whiteColor];
- }
-
- return _collectionView;
- }
- - (UILabel *)tintLable {
- if (!_tintLable) {
- _tintLable = [[UILabel alloc] init];
- _tintLable.font = SYSFONT(16);
- _tintLable.text = [NSString stringWithFormat:Localized(@"JX_NotGalleryPermissions"),APP_NAME];
- _tintLable.textAlignment = NSTextAlignmentCenter;
- _tintLable.numberOfLines = 0;
- _tintLable.hidden = YES;
- }
- return _tintLable;
- }
- #pragma mark - <RITLPhotosCellActionTarget>
- - (void)photosCellDidTouchUpInSlide:(RITLPhotosCell *)cell asset:(PHAsset *)asset indexPath:(NSIndexPath *)indexPath complete:(RITLPhotosCellStatusAction)animated
- {
- if (self.dataManager.count >= RITLPhotosConfiguration.defaultConfiguration.maxCount &&
- ![self.dataManager containAsset:asset]/*是添加*/) {
-
- [g_server showMsg:Localized(@"JX_CannotSelectMorePhotos")];
-
- return;
-
- }//不能进行选择
-
- NSInteger index = [self.dataManager addOrRemoveAsset:asset].integerValue;
-
- animated(RITLPhotosCellAnimatedStatusPermit,index > 0,MAX(0,index));
-
- if (index < 0 && !self.isEdit) {//表示进行了取消操作
- [self.collectionView reloadData];
- }
- }
- #pragma mark - <KVO>
- - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
- {
- if ([keyPath isEqualToString:@"count"] && [object isEqual:self.dataManager]) {
-
- [self changedBottomViewStatus];
- }
-
- else if([keyPath isEqualToString:@"hightQuality"] && [object isEqual:self.dataManager]){
-
- BOOL hightQuality = [change[NSKeyValueChangeNewKey] boolValue];
- self.bottomView.fullImageButton.selected = hightQuality;
- }
- }
- #pragma mark - 检测
- /// 检测底部视图的状态
- - (void)changedBottomViewStatus
- {
- NSInteger count = self.dataManager.count;
-
- self.bottomView.previewButton.enabled = !(count == 0);
- UIControlState state = (count == 0 ? UIControlStateDisabled : UIControlStateNormal);
- NSString *title;
- if(!RITLPhotosConfiguration.defaultConfiguration.isRichScan){
- title = (count == 0 ? Localized(@"JX_Send") : [NSString stringWithFormat:@"%@(%@)",Localized(@"JX_Send"),@(count)]);
- }else {
- title = Localized(@"JX_Finish");
- }
- [self.bottomView.sendButton setTitle:title forState:state];
- self.bottomView.sendButton.enabled = !(count == 0);
- }
- #pragma mark - action
- - (void)hightQualityShouldChanged:(UIButton *)sender
- {
- self.dataManager.hightQuality = !self.dataManager.hightQuality;
- }
- @end
|