123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720 |
- //
- // XG_AssetPickerController.m
- // MyApp
- //
- // Created by huxinguang on 2018/9/26.
- // Copyright © 2018年 huxinguang. All rights reserved.
- //
- #import "XG_AssetPickerController.h"
- #import "XG_AssetCell.h"
- #import "XG_TitleView.h"
- #import "XG_BarButton.h"
- #import "XG_AlbumCell.h"
- #import <MobileCoreServices/MobileCoreServices.h>
- #import "XG_MediaBrowseView.h"
- #import "FLAnimatedImage.h"
- @interface XG_AssetPickerController ()<UICollectionViewDataSource,UICollectionViewDelegate,UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate, UIImagePickerControllerDelegate,PHPhotoLibraryChangeObserver>
- @property (nonatomic, strong) UICollectionView *collectionView;
- @property (nonatomic, strong) NSMutableArray<XG_AssetModel *> *assetArr;
- @property (nonatomic, strong) UIButton *bottomConfirmBtn;
- @property (nonatomic, strong) UITableView *albumTableView;
- @property (nonatomic, strong) UIView *containerView;
- @property (nonatomic, strong) NSMutableArray<XG_AlbumModel *> *albumArr;
- @property (nonatomic, strong) UIControl *mask;
- @property (nonatomic, strong) NavTitleView *ntView;
- @property (nonatomic, assign) CGFloat containerViewHeight;
- @property (nonatomic, strong) NSIndexPath *currentAlbumIndexpath;
- @property (nonatomic, strong) XG_AssetModel *placeholderModel; //相机占位model
- @property (nonatomic, strong) NSMutableArray<NSIndexPath *> *albumSelectedIndexpaths;
- @property (nonatomic, strong) NSLayoutConstraint *containerView_bottom;
- @property (nonatomic, assign) BOOL hideStatusBar;
- @property (nonatomic, strong) NSTimer * timer;
- @property (nonatomic) int numTime;
- @end
- @implementation XG_AssetPickerController
- @synthesize assetArr = _assetArr;//同时重写setter/getter方法需要这样
- -(XG_AssetModel *)placeholderModel{
- if (_placeholderModel == nil) {
- _placeholderModel = [[XG_AssetModel alloc]init];
- _placeholderModel.isPlaceholder = YES;
- }
- return _placeholderModel;
- }
- -(NSMutableArray<XG_AlbumModel *> *)albumArr{
- if (_albumArr == nil) _albumArr = [NSMutableArray array];
- return _albumArr;
- }
- -(NSMutableArray<XG_AssetModel *> *)assetArr{
- if (_assetArr == nil) _assetArr = [NSMutableArray array];
- return _assetArr;
- }
- - (NSMutableArray<NSIndexPath *> *)albumSelectedIndexpaths{
- if (_albumSelectedIndexpaths == nil) _albumSelectedIndexpaths = [NSMutableArray array];
- return _albumSelectedIndexpaths;
- }
- -(void)setAssetArr:(NSMutableArray<XG_AssetModel *> *)assetArr{
- _assetArr = assetArr;
- //插入相机占位
- if (![_assetArr containsObject:self.placeholderModel]) {
- [_assetArr insertObject:self.placeholderModel atIndex:0];
- }
- }
- -(void)dealloc{
- [[PHPhotoLibrary sharedPhotoLibrary] unregisterChangeObserver:self];
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- -(instancetype)initWithOptions:(XG_AssetPickerOptions *)options delegate:(id<XG_AssetPickerControllerDelegate>)delegate{
- if (self = [super init]) {
- self.pickerOptions = options;
- self.delegate = delegate;
- }
- return self;
- }
- //- (BOOL)prefersStatusBarHidden{
- // return self.hideStatusBar;
- //}
- - (UIStatusBarAnimation)preferredStatusBarUpdateAnimation{
- return UIStatusBarAnimationFade;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor whiteColor];
- [[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];
- // [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(updateStatusBar) name:kShowStatusBarNotification object:nil];
- [self configMask];
- [self getAlbums];
-
- }
- - (void)getAlbums{
- @weakify(self)
- [[XG_AssetPickerManager manager] getAllAlbums:self.pickerOptions.videoPickable completion:^(NSArray<XG_AlbumModel *> *models) {
- @strongify(self)
- if (!self) return;
- self.albumArr = [NSMutableArray arrayWithArray:models];
- self.albumArr[0].isSelected = YES;//默认第一个选中
- [self.ntView resetViewWithStr:self.albumArr[0].name];
- self.currentAlbumIndexpath = [NSIndexPath indexPathForRow:0 inSection:0];
- self.assetArr = self.albumArr[0].assetArray;
- [self refreshAlbumAssetsStatus];
- [self configCollectionView];
- [self configBottomConfirmBtn];
- [self configAlbumTableView];
- [self addConstraints];
- [self.collectionView reloadData];
- [self refreshNavRightBtn];
- [self.albumTableView reloadData];
- }];
- }
- - (void)resetAlbums{
- @weakify(self)
- [[XG_AssetPickerManager manager] getAllAlbums:self.pickerOptions.videoPickable completion:^(NSArray<XG_AlbumModel *> *models) {
- @strongify(self)
- if (!self) return;
- self.albumArr = [NSMutableArray arrayWithArray:models];
- self.albumArr[self.currentAlbumIndexpath.row].isSelected = YES;
- [self.ntView resetViewWithStr:self.albumArr[self.currentAlbumIndexpath.row].name];
- self.assetArr = self.albumArr[self.currentAlbumIndexpath.row].assetArray;
- [self refreshAlbumAssetsStatus];
- [self refreshNavRightBtn];
- [self refreshBottomConfirmBtn];
- [self.albumTableView reloadData];
- }];
- }
- - (void)configMask{
- self.mask = [UIControl new];
- self.mask.frame = self.view.bounds;
- self.mask.backgroundColor = [UIColor clearColor];
- self.mask.userInteractionEnabled = NO;
- [self.view addSubview:self.mask];
- [self.mask addTarget:self action:@selector(onClickMask) forControlEvents:UIControlEventTouchUpInside];
- }
- - (void)configTitleView{
- self.ntView = [[NavTitleView alloc]init];
- self.ntView.frame = CGRectMake(0, STATUSBAR_HEIGHT, SCREEN_WIDTH, NAVIGATIONBAR_HEIGHT - STATUSBAR_HEIGHT);
- self.ntView.titleBtn.selected = NO;
- [self.ntView.titleBtn addTarget:self action:@selector(onTitleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
- WEAKSELF
- self.ntView.blockRight = ^{
- [weakSelf onConfirmBtnClick];
- };
- self.ntView.blockLeft = ^{
- [weakSelf onLeftBarButtonClick];
- };
- [self.view addSubview:self.ntView];
- }
- //取消
- - (void)onLeftBarButtonClick{
- if (self.delegate && [self.delegate respondsToSelector:@selector(assetPickerControllerDidCancel:)]) {
- [self.delegate assetPickerControllerDidCancel:self];
- }
- [GB_Nav popViewControllerAnimated:true];
- }
- - (void)configCollectionView {
- if (!self.collectionView) {
- UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
- CGFloat itemWH = (self.view.frame.size.width - (kItemsAtEachLine-1)*kItemMargin)/kItemsAtEachLine;
- layout.itemSize = CGSizeMake(itemWH, itemWH);
- layout.minimumInteritemSpacing = kItemMargin;
- layout.minimumLineSpacing = kItemMargin;
- self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
- self.collectionView.backgroundColor = [UIColor whiteColor];
- self.collectionView.dataSource = self;
- self.collectionView.delegate = self;
- self.collectionView.alwaysBounceVertical = YES;
- [self.view addSubview:self.collectionView];
- [self.collectionView registerNib:[UINib nibWithNibName:@"XG_AssetCell" bundle:nil] forCellWithReuseIdentifier:@"XG_AssetCell"];
- }
- }
- - (void)configAlbumTableView{
- if (!self.albumTableView) {
- self.containerViewHeight = SCREEN_HEIGHT-NAVIGATIONBAR_HEIGHT;
- self.containerView = [UIView new];
- self.containerView.backgroundColor = [UIColor whiteColor];
- [self.view addSubview:self.containerView];
-
- self.albumTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
- self.albumTableView.delegate = self;
- self.albumTableView.dataSource = self;
- self.albumTableView.rowHeight = kAlbumTableViewRowHeight;
- self.albumTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
- [self.containerView addSubview:self.albumTableView];
- }
- }
- - (void)configBottomConfirmBtn {
- if (!self.bottomConfirmBtn) {
- self.bottomConfirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- self.bottomConfirmBtn.backgroundColor = [UIColor whiteColor];
- self.bottomConfirmBtn.titleLabel.font = [UIFont boldSystemFontOfSize:kBottomConfirmBtnTitleFontSize];
- // [self.bottomConfirmBtn addTarget:self action:@selector(onConfirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
- // [self.bottomConfirmBtn setTitle:[NSString stringWithFormat:@"确定(%d/%d)",(int)self.pickerOptions.pickedAssetModels.count,(int)self.pickerOptions.maxAssetsCount] forState:UIControlStateNormal];
- [self.bottomConfirmBtn setTitleColor:kAppThemeColor forState:UIControlStateNormal];
- [self.bottomConfirmBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
- if (self.pickerOptions.pickedAssetModels.count > 0) {
- self.bottomConfirmBtn.enabled = YES;
- }else{
- self.bottomConfirmBtn.enabled = NO;
- }
- [self.view addSubview:self.bottomConfirmBtn];
- }
-
- }
- - (void)addConstraints{
- self.collectionView.translatesAutoresizingMaskIntoConstraints = NO;
- self.bottomConfirmBtn.translatesAutoresizingMaskIntoConstraints = NO;
- self.containerView.translatesAutoresizingMaskIntoConstraints = NO;
- self.albumTableView.translatesAutoresizingMaskIntoConstraints = NO;
- self.containerView_bottom = [NSLayoutConstraint constraintWithItem:self.containerView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
- [NSLayoutConstraint activateConstraints:@[
- // collectionView约束
- [NSLayoutConstraint constraintWithItem:self.collectionView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:kAppStatusBarAndNavigationBarHeight],
- [NSLayoutConstraint constraintWithItem:self.collectionView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0],
- [NSLayoutConstraint constraintWithItem:self.collectionView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.bottomConfirmBtn attribute:NSLayoutAttributeTop multiplier:1.0 constant:0],
- [NSLayoutConstraint constraintWithItem:self.collectionView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0],
- // bottomConfirmBtn约束
- [NSLayoutConstraint constraintWithItem:self.bottomConfirmBtn attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0],
- [NSLayoutConstraint constraintWithItem:self.bottomConfirmBtn attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0],
- [NSLayoutConstraint constraintWithItem:self.bottomConfirmBtn attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-kAppTabbarSafeBottomMargin],
- [NSLayoutConstraint constraintWithItem:self.bottomConfirmBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:kBottomConfirmBtnHeight],
- // containerView约束
- [NSLayoutConstraint constraintWithItem:self.containerView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0],
- self.containerView_bottom,
- [NSLayoutConstraint constraintWithItem:self.containerView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0],
- [NSLayoutConstraint constraintWithItem:self.containerView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:self.containerViewHeight],
- // albumTableView约束
- [NSLayoutConstraint constraintWithItem:self.albumTableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.containerView attribute:NSLayoutAttributeTop multiplier:1.0 constant:kAlbumTableViewMarginTopBottom],
- [NSLayoutConstraint constraintWithItem:self.albumTableView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.containerView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0],
- [NSLayoutConstraint constraintWithItem:self.albumTableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.containerView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-kAlbumTableViewMarginTopBottom],
- [NSLayoutConstraint constraintWithItem:self.albumTableView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.containerView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]
- ]];
-
- }
- //下一步
- - (void)onConfirmBtnClick {
- [self timerStop];
- [MBProgressHUD showHUDAddedTo:self.view animated:YES];
- //启动定时器
- self.numTime = 0;
- [self timerStart];
- NSLog(@"1---");
- }
- #pragma mark 定时器相关
- - (void)timerStart{
- //开启定时器
- if (_timer == nil) {
- _timer =[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];
- }
-
- }
- - (void)timerRun{
- //每秒的动作
- if (_numTime <=0) {
- //刷新按钮 开始
- [self timerStop];
- if (self.delegate && [self.delegate respondsToSelector:@selector(assetPickerController:didFinishPickingAssets:)]) {
- [self.delegate assetPickerController:self didFinishPickingAssets:[self.pickerOptions.pickedAssetModels copy]];
- }
- NSLog(@"2---");
- [GB_Nav popViewControllerAnimated:true];
- NSLog(@"3---");
- return;
- }
- _numTime --;
-
- }
- - (void)timerStop{
- //停止定时器
- if (_timer != nil) {
- [_timer invalidate];
- self.timer = nil;
- }
-
- [MBProgressHUD hideHUDForView:self.view animated:YES];
-
- }
- //标题
- - (void)onTitleBtnClick:(UIButton *)btn{
- btn.selected = !btn.selected;
- if (btn.selected) {
- self.containerView_bottom.constant = self.containerViewHeight + kAppStatusBarAndNavigationBarHeight;
- [self.view insertSubview:self.mask belowSubview:self.containerView];
- self.mask.userInteractionEnabled = YES;
- }else{
- self.containerView_bottom.constant = -self.containerViewHeight;
- self.mask.userInteractionEnabled = NO;
- }
- [self.albumTableView reloadData];
- [UIView animateWithDuration:0.35 animations:^{
- if (btn.selected) {
- CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI);
- self.ntView.arrowView.transform = transform;
- self.mask.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.2];
- }else{
- self.ntView.arrowView.transform = CGAffineTransformIdentity;
- self.mask.backgroundColor = [UIColor clearColor];
- }
- [self.view layoutIfNeeded];
- } completion:^(BOOL finished) {
- [self.albumTableView scrollToRowAtIndexPath:self.currentAlbumIndexpath atScrollPosition:UITableViewScrollPositionNone animated:NO];
- if (!btn.selected) {
- [self.view insertSubview:self.mask belowSubview:self.collectionView];
- }
- }];
- }
- - (void)onClickMask{
- [self onTitleBtnClick:self.ntView.titleBtn];
- }
- #pragma mark - UICollectionViewDataSource && Delegate
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- return self.assetArr.count;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- XG_AssetCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XG_AssetCell" forIndexPath:indexPath];
- XG_AssetModel *model = self.assetArr[indexPath.row];
- cell.model = model;
- __weak typeof(cell) weakCell = cell;
- @weakify(self)
- cell.didSelectPhotoBlock = ^(BOOL isSelected) {
- @strongify(self)
- if (!self) return;
- if (isSelected) {
- // 1. 取消选择
- weakCell.selectPhotoButton.selected = NO;
- model.picked = NO;
- model.number = 0;
- for (XG_AssetModel *am in [self.pickerOptions.pickedAssetModels copy]) {
- if ([am.asset.localIdentifier isEqualToString:model.asset.localIdentifier]) {
- am.number = 0;
- am.picked = NO;
- [self.pickerOptions.pickedAssetModels removeObject:am];
- }
- }
- weakCell.numberLabel.text = @"";
- } else {
- // 2. 选择照片,检查是否超过了最大个数的限制
- if (self.pickerOptions.pickedAssetModels.count < self.pickerOptions.maxAssetsCount) {
- weakCell.selectPhotoButton.selected = YES;
- model.picked = YES;
- [self.pickerOptions.pickedAssetModels addObject:model];
- weakCell.numberLabel.text = [NSString stringWithFormat:@"%d",(int)self.pickerOptions.pickedAssetModels.count];
- } else {
- [GlobalMethod showAlert:[NSString stringWithFormat:@"最多选择%d张照片",(int)self.pickerOptions.maxAssetsCount]];
- }
- }
- [self refreshAlbumAssetsStatus];
- //取消选择的时候才刷新所有选中的item
- if (self.albumSelectedIndexpaths.count > 0 && isSelected) {
- [collectionView reloadItemsAtIndexPaths:self.albumSelectedIndexpaths];
- }
- [self refreshNavRightBtn];
- [self refreshBottomConfirmBtn];
-
- };
- return cell;
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- if (indexPath.item == 0) {
- //打开相机
- if (self.pickerOptions.pickedAssetModels.count < self.pickerOptions.maxAssetsCount) {
- #if TARGET_OS_SIMULATOR
- [GlobalMethod showAlert:@"模拟器不支持相机"];
- #else
- [self openCamera];
- #endif
- }else{
- [GlobalMethod showAlert:[NSString stringWithFormat:@"最多选择%d张照片",(int)self.pickerOptions.maxAssetsCount]];
- }
- }else{
- NSMutableArray *items = [self.assetArr mutableCopy];
- [items removeObjectAtIndex:0];
- // [self performSelector:@selector(updateStatusBar) withObject:nil afterDelay:0.2];
- XG_MediaBrowseView *v = [[XG_MediaBrowseView alloc] initWithItems:items];
- [v presentCellImageAtIndexPath:indexPath FromCollectionView:collectionView toContainer:self.navigationController.view animated:YES completion:nil];
- }
- }
- #pragma mark - UITableViewDelegate,UITableViewDataSource
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- return self.albumArr.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- static NSString *cellIdentifier = @"CellIdentifier";
- XG_AlbumCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
- if (!cell) {
- cell = [[[NSBundle mainBundle]loadNibNamed:@"XG_AlbumCell" owner:self options:nil] lastObject];
- }
- cell.model = self.albumArr[indexPath.row];
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- for (XG_AlbumModel *album in self.albumArr) {
- album.isSelected = NO;
- }
- self.albumArr[indexPath.row].isSelected = YES;
- [self.ntView resetViewWithStr:self.albumArr[indexPath.row].name];
- self.assetArr = self.albumArr[indexPath.row].assetArray;
- if (indexPath != self.currentAlbumIndexpath) {
- [self refreshAlbumAssetsStatus];
- [self.collectionView reloadData];
- [self onTitleBtnClick:self.ntView.titleBtn];
- }else{
- [self onTitleBtnClick:self.ntView.titleBtn];
- }
- self.currentAlbumIndexpath = indexPath;
- }
- - (void)refreshAlbumAssetsStatus{
- [self.albumSelectedIndexpaths removeAllObjects];
- for (int i=1; i<self.assetArr.count; i++) {//第1个为相机占位
- XG_AssetModel *am = self.assetArr[i];
- am.picked = NO;
- am.number = 0;
- for (int j=0; j<self.pickerOptions.pickedAssetModels.count; j++) {
- XG_AssetModel *pam = self.pickerOptions.pickedAssetModels[j];
- if ([am.asset.localIdentifier isEqualToString:pam.asset.localIdentifier]) {
- am.picked = YES;
- am.number = j+1;
- [self.albumSelectedIndexpaths addObject:[NSIndexPath indexPathForItem:i inSection:0]];
- }
- }
- }
- }
- - (void)refreshNavRightBtn{
- XG_BarButton *btn = (XG_BarButton *)self.navigationItem.rightBarButtonItem.customView;
- if (self.pickerOptions.pickedAssetModels.count > 0) {
- btn.enabled = YES;
- }else{
- btn.enabled = NO;
- }
- }
- - (void)refreshBottomConfirmBtn {
- if (self.pickerOptions.pickedAssetModels.count > 0) {
- self.bottomConfirmBtn.enabled = YES;
- }else{
- self.bottomConfirmBtn.enabled = NO;
- }
- // [self.bottomConfirmBtn setTitle:[NSString stringWithFormat:@"确定(%d/%d)",(int)self.pickerOptions.pickedAssetModels.count,(int)self.pickerOptions.maxAssetsCount] forState:UIControlStateNormal];
- }
- //- (void)updateStatusBar{
- // self.hideStatusBar = !self.hideStatusBar;
- // [self performSelectorOnMainThread:@selector(setNeedsStatusBarAppearanceUpdate) withObject:nil waitUntilDone:YES];
- //}
- - (void)showHudWithString:(NSString *)string{
-
- }
- - (void)openCamera{
- UIImagePickerController *picker = [[UIImagePickerController alloc] init];
- picker.sourceType = UIImagePickerControllerSourceTypeCamera;
- if (self.pickerOptions.videoPickable) {
- NSString *mediaTypeImage = (NSString *)kUTTypeImage;
- NSString *mediaTypeMovie = (NSString *)kUTTypeMovie;
- picker.mediaTypes = @[mediaTypeImage,mediaTypeMovie];
- }
- picker.delegate = self;
- picker.navigationBar.barTintColor = self.navigationController.navigationBar.barTintColor;
- picker.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],
- NSFontAttributeName : [UIFont boldSystemFontOfSize:18]};
- [self presentViewController:picker animated:YES completion:nil];
-
- }
- - (void)switchToCameraRoll{
- for (XG_AlbumModel *album in self.albumArr) {
- album.isSelected = NO;
- }
- self.albumArr[0].isSelected = YES;
- // [self.ntView.titleBtn setTitle:self.albumArr[0].name forState:UIControlStateNormal];
- [self.ntView resetViewWithStr:self.albumArr[0].name];
- // self.ntView.titleBtnWidth = [self getTitleBtnWidthWithTitle:self.albumArr[0].name];
- self.assetArr = self.albumArr[0].assetArray;
- [self refreshAlbumAssetsStatus];
- [self.collectionView reloadData];
- [self.albumTableView reloadData];
- self.currentAlbumIndexpath = [NSIndexPath indexPathForRow:0 inSection:0];
- }
- - (CGFloat)getTitleBtnWidthWithTitle:(NSString *)title{
- NSMutableDictionary *attr = [NSMutableDictionary new];
- attr[NSFontAttributeName] = kTitleViewTitleFont;
- CGRect rect = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT)
- options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
- attributes:attr context:nil];
-
- return rect.size.width + kTitleViewTextImageDistance + kTitleViewArrowSize.width;
- }
- #pragma mark - PHPhotoLibraryChangeObserver
- - (void)photoLibraryDidChange:(PHChange *)changeInstance{
- @weakify(self)
- dispatch_sync(dispatch_get_main_queue(), ^{
- @strongify(self)
- if (!self) return;
- //相册添加照片所产生的change(这里只对app内调用相机拍照后点击“use photo(使用照片)”按钮后所产生的change)
- XG_AlbumModel *currentAlbum = self.albumArr[0];
- PHFetchResultChangeDetails *changes = [changeInstance changeDetailsForFetchResult:currentAlbum.result];
- if (changes) {
- currentAlbum.result = [changes fetchResultAfterChanges];
- if (changes.hasIncrementalChanges) {
- if (self.collectionView) {
- NSArray<PHAsset *> *insertItems = changes.insertedObjects;
- NSMutableArray *indexPaths = @[].mutableCopy;
- if (insertItems && insertItems.count > 0) {
- for (int i=0; i<insertItems.count; i++) {
- XG_AssetModel *model = [[XG_AssetModel alloc] init];
- model.asset = insertItems[i];
- if (self.pickerOptions.pickedAssetModels.count < self.pickerOptions.maxAssetsCount) {
- model.picked = YES;
- model.number = (int)self.pickerOptions.pickedAssetModels.count + 1;
- [self.pickerOptions.pickedAssetModels addObject:model];
- }else{
- model.picked = NO;
- model.number = 0;
- }
- [currentAlbum.assetArray insertObject:model atIndex:1];
- [indexPaths addObject:[NSIndexPath indexPathForItem:i+1 inSection:0]];
- }
- }
- [self.collectionView performBatchUpdates:^{
- NSArray<PHAsset *> *insertItems = changes.insertedObjects;
- if (insertItems && insertItems.count > 0) {
- [self.collectionView insertItemsAtIndexPaths:indexPaths];
- [changes enumerateMovesWithBlock:^(NSUInteger fromIndex, NSUInteger toIndex) {
- [self.collectionView moveItemAtIndexPath:[NSIndexPath indexPathForItem:fromIndex inSection:0] toIndexPath:[NSIndexPath indexPathForItem:toIndex inSection:0]];
- }];
- [self.collectionView reloadItemsAtIndexPaths:indexPaths];
- }
- } completion:^(BOOL finished) {
- [self resetAlbums];
- }];
- }
- }
- }
-
- });
-
- }
- #pragma mark - UIImagePickerControllerDelegate
- - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
- NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
- if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
- UIImage *image = info[UIImagePickerControllerOriginalImage];
- UIImageWriteToSavedPhotosAlbum(image, self, @selector(image: didFinishSavingWithError: contextInfo:), nil);
- }else{
- NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
- NSString *urlStr = [url path];
- if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlStr)) {
- UISaveVideoAtPathToSavedPhotosAlbum(urlStr, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
- }
- }
- if (self.currentAlbumIndexpath.row != 0) {
- [self switchToCameraRoll];
- }
- [picker dismissViewControllerAnimated:YES completion:nil];
- }
- - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
- [picker dismissViewControllerAnimated:YES completion:nil];
- }
- - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
- }
- - (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
-
- }
- @end
- @implementation NavTitleView
- #pragma mark 懒加载
- -(UIButton *)titleBtn{
- if (_titleBtn == nil) {
- _titleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- self.titleBtn.titleLabel.font = kTitleViewTitleFont;
- [self.titleBtn setTitleColor:COLOR_LABEL forState:UIControlStateNormal];
- _titleBtn.widthHeight = XY(W(100),W(40));
- }
- return _titleBtn;
- }
- - (UIImageView *)arrowView{
- if (_arrowView == nil) {
- _arrowView = [UIImageView new];
- _arrowView.image = [UIImage imageNamed:@"down-black"];
- _arrowView.widthHeight = XY(W(12),W(7));
- }
- return _arrowView;
- }
- -(XG_BarButton *)leftBarButton{
- if (_leftBarButton == nil) {
- XG_BarButtonConfiguration *config = [[XG_BarButtonConfiguration alloc]init];
- config.type = XG_BarButtonTypeImage;
- config.normalImageName = @"cancel-deep";
- _leftBarButton = [[XG_BarButton alloc]initWithConfiguration:config];
- [_leftBarButton addTarget:self action:@selector(onLeftBarButtonClick) forControlEvents:UIControlEventTouchUpInside];
- _leftBarButton.widthHeight = XY(W(40),W(40));
- }
- return _leftBarButton;
- }
- -(UIButton *)rightBarButton{
- if (_rightBarButton == nil) {
- XG_BarButtonConfiguration *config = [[XG_BarButtonConfiguration alloc]init];
- config.type = XG_BarButtonTypeText;
- config.titleString = @"下一步";
- config.normalColor = kAppThemeColor;
- config.disabledColor = [UIColor lightGrayColor];
- config.titleFont = [UIFont boldSystemFontOfSize:15];
- _rightBarButton = [[XG_BarButton alloc]initWithConfiguration:config];
- [_rightBarButton addTarget:self action:@selector(onConfirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
- _rightBarButton.widthHeight = XY(W(75),W(28));
- // _rightBarButton.backgroundColor = COLOR_BLUE;
- [GlobalMethod setRoundView:_rightBarButton color:[UIColor clearColor] numRound:20 width:0];
- }
- return _rightBarButton;
- }
- #pragma mark 初始化
- - (instancetype)initWithFrame:(CGRect)frame{
- self = [super initWithFrame:frame];
- if (self) {
- self.backgroundColor = [UIColor whiteColor];
- self.width = SCREEN_WIDTH;
- [self addSubView];
- }
- return self;
- }
- //添加subview
- - (void)addSubView{
- [self addSubview:self.titleBtn];
- [self addSubview:self.arrowView];
- [self addSubview:self.leftBarButton];
- [self addSubview:self.rightBarButton];
- //初始化页面
- [self resetViewWithStr:nil];
- }
- #pragma mark 刷新view
- - (void)resetViewWithStr:(NSString *)str{
- [self removeSubViewWithTag:TAG_LINE];//移除线
- //刷新view
- [_titleBtn setTitle:UnPackStr(str) forState:(UIControlStateNormal)];
- self.titleBtn.centerXCenterY = XY(SCREEN_WIDTH/2,self.height/2);
- self.arrowView.leftCenterY = XY(self.titleBtn.right+W(10),self.titleBtn.centerY);
-
- self.leftBarButton.leftCenterY = XY(W(10),self.titleBtn.centerY);
- self.rightBarButton.rightCenterY = XY(SCREEN_WIDTH-W(10),self.titleBtn.centerY);
- self.height = NAVIGATIONBAR_HEIGHT-STATUSBAR_HEIGHT;
- }
- - (void)onLeftBarButtonClick{
- if (self.blockLeft) {
- self.blockLeft();
- }
- }
- - (void)onConfirmBtnClick{
- if (self.blockRight) {
- self.blockRight();
- }
- }
- @end
- @implementation XG_AssetPickerOptions
- -(NSMutableArray<XG_AssetModel *> *)pickedAssetModels{
- if(_pickedAssetModels == nil) _pickedAssetModels = [NSMutableArray array];
- return _pickedAssetModels;
- }
- @end
|