This commit is contained in:
lianxiang
2018-09-23 01:30:29 +08:00
parent db1740e72b
commit 3508cab25e
80 changed files with 3687 additions and 127 deletions
@@ -0,0 +1,14 @@
//
// SJPhotoAlbumsController.h
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/22.
// Copyright © 2016年 S.J. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <Photos/Photos.h>
@interface SJPhotoAlbumsController : UIViewController
@end
@@ -0,0 +1,135 @@
//
// SJPhotoAlbumsController.m
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/22.
// Copyright © 2016年 S.J. All rights reserved.
//
#import "SJPhotoAlbumsController.h"
#import "SJPhotoPickerMacro.h"
#import "SJPhotoAlbumCell.h"
#import "SJPickPhotoController.h"
#import "SJPhotoPickerManager.h"
#import "SJAlbumModel.h"
@interface SJPhotoAlbumsController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
/**
* 相册数组
*/
@property (nonatomic, strong) NSMutableArray *albumArray;
@end
static NSString *ID_SJPhotoAlbumCell = @"sJPhotoAlbumCell";
@implementation SJPhotoAlbumsController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupDatas];
[self setupViews];
SJPickPhotoController *vc = [[SJPickPhotoController alloc] init];
SJAlbumModel *model = [[SJAlbumModel alloc] init];
model = self.albumArray[0];
vc.assetResult = model.assetResult;
[self.navigationController pushViewController:vc animated:NO];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark 数据初始化
- (void)setupDatas {
[[SJPhotoPickerManager shareSJPhotoPickerManager] requestAlbumsWithType:PHAssetCollectionTypeSmartAlbum albumResult:^(NSArray *albumArray) {
self.albumArray = [albumArray mutableCopy];
}];
[self.tableView reloadData];
}
#pragma mark 视图初始化
- (void)setupViews {
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightBarBtnAction:)];
// tableView
{
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W,SCREEN_H) style:(UITableViewStyleGrouped)];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView registerClass:[SJPhotoAlbumCell class] forCellReuseIdentifier:ID_SJPhotoAlbumCell];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.tableView];
}
}
#pragma mark- UITableViewDelegate DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.albumArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SJPhotoAlbumCell *cell = [tableView dequeueReusableCellWithIdentifier:ID_SJPhotoAlbumCell forIndexPath:indexPath];
if (!cell) {
cell = [[SJPhotoAlbumCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:ID_SJPhotoAlbumCell];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
SJAlbumModel *model = self.albumArray[indexPath.row];
cell.title = [NSString stringWithFormat:@"%@%lu",model.title,model.assetResult.count];
[[SJPhotoPickerManager shareSJPhotoPickerManager] requestImageForPHAsset:model.assetResult[0] targetSize:CGSizeMake(55, 55) imageResult:^(UIImage *image) {
cell.img = image;
}];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 55;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 5.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.01f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
SJPickPhotoController *vc = [[SJPickPhotoController alloc] init];
SJAlbumModel *model = self.albumArray[indexPath.row];
vc.assetResult = model.assetResult;
vc.title = model.title;
[self.navigationController pushViewController:vc animated:YES];
}
- (void)rightBarBtnAction:(UIBarButtonItem *)sender {
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
@end
@@ -0,0 +1,13 @@
//
// SJPhotoPickerNavController.h
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/22.
// Copyright © 2016年 S.J. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface SJPhotoPickerNavController : UINavigationController
@end
@@ -0,0 +1,38 @@
//
// SJPhotoPickerNavController.m
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/22.
// Copyright © 2016年 S.J. All rights reserved.
//
#import "SJPhotoPickerNavController.h"
#import "SJPhotoAlbumsController.h"
#import "SJPhotoPickerMacro.h"
@interface SJPhotoPickerNavController ()
@end
@implementation SJPhotoPickerNavController
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationBar setBarTintColor:NAVBAR_CLOOUR];
[self.navigationBar setTintColor:[UIColor whiteColor]];
[self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
SJPhotoAlbumsController *vc = [[SJPhotoAlbumsController alloc] init];
vc.title = @"相册";
[self pushViewController:vc animated:NO];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
@@ -0,0 +1,16 @@
//
// SJPickPhotoController.h
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/19.
// Copyright © 2016年 S.J. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <Photos/Photos.h>
@interface SJPickPhotoController : UIViewController
@property (nonatomic, strong) PHFetchResult *assetResult;
@end
@@ -0,0 +1,240 @@
// SJPickPhotoController.m
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/19.
// Copyright © 2016年 S.J. All rights reserved.
//
#import "SJPickPhotoController.h"
#import "SJPhotoPicker.h"
#import "SJPhotoPickerMacro.h"
#import "SJPhotoModel.h"
#import "SJPickPhotoCell.h"
#import "SJPhotoPickerToolBar.h"
#import "SJPreviewPhotoController.h"
@interface SJPickPhotoController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,SJPreviewPhotoControllerDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) SJPhotoPickerToolBar *toolBar;
/**
* 所有照片
*/
@property (nonatomic, strong) NSMutableArray<SJPhotoModel *> *photoArray;
/**
* 选择的照片
*/
@property (nonatomic, strong) NSMutableArray<SJPhotoModel *> *pickedArray;
@end
static NSString *ID_SJPickPhotoCell = @"sJPickPhotoCell";
@implementation SJPickPhotoController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupDatas];
[self setupViews];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark 数据初始化
- (void)setupDatas {
// photoArray
{
self.photoArray = [NSMutableArray array];
for (PHAsset *asset in self.assetResult) {
SJPhotoModel *model = [[SJPhotoModel alloc] init];
model.asset = asset;
model.inAlbumIndex = self.photoArray.count;
model.pickedIndex = 0;
model.isPicked = NO;
[self.photoArray addObject:model];
}
[self.collectionView reloadData];
}
// pickedArray
{
self.pickedArray = [NSMutableArray array];
}
}
#pragma mark 视图初始化
- (void)setupViews {
self.view.backgroundColor = [UIColor whiteColor];
if (self.title.length == 0) {
self.title = @"所有照片";
}
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightBarBtnAction:)];
// collectionView
{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = 5.0f;
layout.minimumInteritemSpacing = 0.0f;
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 5, SCREEN_W, SCREEN_H - 49) collectionViewLayout:layout];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView registerClass:[SJPickPhotoCell class] forCellWithReuseIdentifier:ID_SJPickPhotoCell];
self.collectionView.backgroundColor = [UIColor whiteColor];
self.collectionView.pagingEnabled = NO;
[self.view addSubview:self.collectionView];
}
// toolBar
{
self.toolBar = [[SJPhotoPickerToolBar alloc] init];
[self.toolBar.leftBtn addTarget:self action:@selector(previewPhotoAction:) forControlEvents:(UIControlEventTouchUpInside)];
[self.toolBar.rightBtn addTarget:self action:@selector(sureBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:self.toolBar];
}
}
#pragma mark- UICollectionViewDelegate DataSource
#pragma mark-
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.photoArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
SJPickPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID_SJPickPhotoCell forIndexPath:indexPath];
cell.backgroundColor = [UIColor yellowColor];
SJPhotoModel *model = self.photoArray[indexPath.row];
[cell setModel:model];
__weak typeof(cell) weakCell = cell;
cell.pickBtnBlock = ^(SJPhotoModel *curPickModel) {
[self changeCell:weakCell WithModel:curPickModel];
};
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(Image_W, Image_W);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
SJPreviewPhotoController *vc = [[SJPreviewPhotoController alloc] init];
vc.delegate = self;
vc.curIndex = indexPath.row;
vc.previewArray = [self.photoArray mutableCopy];
[self.navigationController pushViewController:vc animated:YES];
}
#pragma mark- 点击事件
#pragma mark-
#pragma mark 预览
- (void)previewPhotoAction:(UIButton *)sender {
if (self.pickedArray.count > 0) {
SJPreviewPhotoController *vc = [[SJPreviewPhotoController alloc] init];
vc.delegate = self;
vc.curIndex = self.pickedArray.count - 1;
vc.previewArray = [self.pickedArray mutableCopy];
[self.navigationController pushViewController:vc animated:YES];
}
}
#pragma mark 取消
- (void)rightBarBtnAction:(UIBarButtonItem *)sender {
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark 确定
- (void)sureBtnAction:(UIButton *)sender {
NSMutableArray *assetArray = [NSMutableArray array];
for (SJPhotoModel *model in self.pickedArray) {
PHAsset *asset = model.asset;
[assetArray addObject:asset];
}
if (assetArray.count > MAXCOUNT) {
GIGA_ShowToast(@"最多6张");
return;
}
SJPhotoPicker *photoPicker = [SJPhotoPicker shareSJPhotoPicker];
photoPicker.photoPickerBlock(assetArray);
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark- 其他方法
#pragma mark-
#pragma mark 通过改变改变model值,改变cell状态
- (void)pickedArrayChangeWithModel:(SJPhotoModel *)model {
NSIndexPath *changedIndexP =[NSIndexPath indexPathForItem:model.inAlbumIndex inSection:0];
SJPickPhotoCell *changedCell = (SJPickPhotoCell *)[self.collectionView cellForItemAtIndexPath:changedIndexP];
[self changeCell:changedCell WithModel:model];
}
- (void)changeCell:(SJPickPhotoCell *)changedCell WithModel:(SJPhotoModel *)changedModel {
if (changedModel.isPicked) {
[changedCell modelChangePickedIndex:self.pickedArray.count + 1];
[self.pickedArray addObject:changedModel];
}
else {
[self.pickedArray removeObject:changedModel];
// 更新剩下选择的图片
for (int i = 0; i < self.pickedArray.count; i ++) {
SJPhotoModel *otherPickedModel = self.pickedArray[i];
if (otherPickedModel.pickedIndex > changedModel.pickedIndex) {
otherPickedModel.pickedIndex -= 1;
NSIndexPath *indexP =[NSIndexPath indexPathForItem:otherPickedModel.inAlbumIndex inSection:0];
SJPickPhotoCell *otherCell = (SJPickPhotoCell *)[self.collectionView cellForItemAtIndexPath:indexP];
[otherCell modelChangePickedIndex:otherPickedModel.pickedIndex];
}
}
// 更新 取消选择的图片
[changedCell modelChangePickedIndex:0];
}
// 更改工具栏状态信息
[self.toolBar changePickedIndex:self.pickedArray.count];
}
@end
@@ -0,0 +1,27 @@
//
// SJPreviewPhotoController.h
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/19.
// Copyright © 2016年 S.J. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "SJPhotoModel.h"
@protocol SJPreviewPhotoControllerDelegate <NSObject>
- (void)pickedArrayChangeWithModel:(SJPhotoModel *)model;
@end
@interface SJPreviewPhotoController : UIViewController
@property (nonatomic, strong) NSMutableArray *previewArray;
@property (nonatomic, assign) NSInteger curIndex;
@property (nonatomic, strong) id<SJPreviewPhotoControllerDelegate> delegate;
@end
@@ -0,0 +1,215 @@
//
// SJPreviewPhotoController.m
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/19.
// Copyright © 2016年 S.J. All rights reserved.
//
#import "SJPreviewPhotoController.h"
#import <Photos/Photos.h>
#import "SJPhotoPickerNavBar.h"
#import "SJPhotoPickerToolBar.h"
#import "SJPreviewPhotoCell.h"
#import "SJPhotoPicker.h"
@interface SJPreviewPhotoController ()<UICollectionViewDelegate,UICollectionViewDataSource,SJPhotoPickerNavBarDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) SJPhotoPickerToolBar *toolBar;
@property (nonatomic, strong) SJPhotoPickerNavBar *navBar;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) NSMutableArray *pickedArray;
@property (nonatomic, strong) SJPhotoModel *operatingModel;
@end
static NSString *ID_SJPreviewPhotoCell = @"sJPreviewPhotoCell";
@implementation SJPreviewPhotoController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupDatas];
[self setupViews];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:NO];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark 数据初始化
- (void)setupDatas {
self.pickedArray = [NSMutableArray array];
for (SJPhotoModel *model in self.previewArray) {
if (model.isPicked) {
[self.pickedArray addObject:model];
}
}
[self.collectionView reloadData];
}
#pragma mark 视图初始化
- (void)setupViews {
// collectionView
{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = 0.0f;
layout.minimumInteritemSpacing = 0.0f;
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, -64, SCREEN_W, SCREEN_H + 64) collectionViewLayout:layout];
self.collectionView.pagingEnabled = YES;
self.collectionView.showsHorizontalScrollIndicator = NO;
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView registerClass:[SJPreviewPhotoCell class] forCellWithReuseIdentifier:ID_SJPreviewPhotoCell];
self.collectionView.backgroundColor = [UIColor whiteColor];
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.curIndex inSection:0] atScrollPosition:(UICollectionViewScrollPositionNone) animated:NO];
self.operatingModel = [self.previewArray lastObject];
[self.view addSubview:self.collectionView];
}
// nav
{
self.navBar = [[SJPhotoPickerNavBar alloc] init];
self.navBar.delegate = self;
self.operatingModel = self.previewArray[self.curIndex];
if (self.title.length == 0) {
self.navBar.leftBtnTitle = @"";
}
self.navBar.barTitle = [NSString stringWithFormat:@"%lu/%lu",(unsigned long)self.curIndex + 1,(unsigned long)self.previewArray.count];
if (self.operatingModel.isPicked) {
[self.navBar setRightBtnTitleIndex:self.operatingModel.pickedIndex];
}
[self.view addSubview:self.navBar];
}
// toolBar
{
self.toolBar = [[SJPhotoPickerToolBar alloc] init];
self.toolBar.backgroundColor = [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:0.3];
self.toolBar.rightBtn.backgroundColor = NAVBAR_CLOOUR;
[self.toolBar.rightBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
self.toolBar.leftBtn.hidden = YES;
[self.toolBar.rightBtn addTarget:self action:@selector(sureBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:self.toolBar];
}
}
#pragma mark- UICollectionViewDelegate DataSource
#pragma mark-
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.previewArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
SJPreviewPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID_SJPreviewPhotoCell forIndexPath:indexPath];
cell.zoomScale = 1.0;
SJPhotoModel *model = self.previewArray[indexPath.item];
cell.asset = model.asset;
cell.singleTapEvent = ^(){
[self.toolBar changeHideState];
[self.navBar changeHideState];
};
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(SCREEN_W, SCREEN_H);
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSInteger index = (NSInteger)scrollView.contentOffset.x / SCREEN_W;
self.operatingModel = self.previewArray[index];
self.navBar.barTitle = [NSString stringWithFormat:@"%lu/%lu",index + 1,(unsigned long)self.previewArray.count];
[self.navBar setRightBtnTitleIndex:self.operatingModel.pickedIndex];
}
#pragma mark- 点击事件
#pragma mark-
#pragma mark 单选框点击(SJ...NavBarDelegate
- (void)rightBtnAction:(UIButton *)sender {
self.operatingModel.isPicked = !self.operatingModel.isPicked;
// 代理传值
[self.delegate pickedArrayChangeWithModel:self.operatingModel];
if (self.operatingModel.isPicked) {
NSInteger pickedIndex = self.pickedArray.count + 1;
self.operatingModel.pickedIndex = pickedIndex;
[self.pickedArray addObject:self.operatingModel];
[self.navBar setRightBtnTitleIndex:pickedIndex];
}
else {
[self.pickedArray removeObject:self.operatingModel];
self.operatingModel.pickedIndex = 0;
for (int i = 0; i < self.pickedArray.count; i ++) {
SJPhotoModel *model = self.pickedArray[i];
model.pickedIndex = i + 1;
}
[self.navBar setRightBtnTitleIndex:0];
}
}
#pragma mark 确定按钮
- (void)sureBtnAction:(UIButton *)sender {
NSMutableArray *assetArray = [NSMutableArray array];
for (SJPhotoModel *model in self.pickedArray) {
PHAsset *asset = model.asset;
[assetArray addObject:asset];
}
if (assetArray.count > MAXCOUNT) {
GIGA_ShowToast(@"最多6张");
return;
}
SJPhotoPicker *photoPicker = [SJPhotoPicker shareSJPhotoPicker];
photoPicker.photoPickerBlock(assetArray);
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
@end