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
+1 -2
View File
@@ -228,11 +228,10 @@
if (jsonString == nil) {
return nil;
}
//⚠️ 13.2.0库编码采用 isoLatin1
//⚠️
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
//NSData *jsonData = [jsonString dataUsingEncoding:NSISOLatin1StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
+6 -1
View File
@@ -22,10 +22,15 @@ typedef void (^responseBlock)(NSDictionary *responseDict, NSDictionary *response
// 测试题 , 登录 body 传参
+(void)userbodyRequest:(NSString *)url params:(NSDictionary *)param completionHandler:(nullable void (^)(NSURLResponse *response, NSDictionary *resDic, NSError * _Nullable error))completionHandler;
//上传图片
//上传图片 1张
+(void)uploadImage:(NSString *)url
imgData:(NSData *)imgData
parms:(NSDictionary *)parms
responseBlock:(responseBlock)block;
+(void)uploadImage:(NSString *)url
imageNames:(NSArray *)imageNames
parms:(NSDictionary *)parms
responseBlock:(responseBlock)block;
@end
+53 -4
View File
@@ -10,7 +10,7 @@
#import "AFNetworking.h"
#import "GiGaServerConfig.h"
#import "GiGaUserDefault.h"
#import "GIGaUserFileHelper.h"
@interface GiGaNetManager()
@end
@@ -130,7 +130,7 @@
[task resume];
}
//上传1张
+(void)uploadImage:(NSString *)url
imgData:(NSData *)imgData
parms:(NSDictionary *)parms
@@ -182,6 +182,55 @@
}];
}
//上传多张
+(void)uploadImage:(NSString *)url
imageNames:(NSArray *)imageNames
parms:(NSDictionary *)parms
responseBlock:(responseBlock)block
{
AFHTTPSessionManager*manager=[[AFHTTPSessionManager alloc] init];
manager.responseSerializer=[AFHTTPResponseSerializer serializer];
NSString *token = [GiGaUserDefault getCurentToken];
if (token) {
[manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
}
NSString *fullurl = [NSString stringWithFormat:@"%@%@",[GiGaServerConfig getMainUrl],url];
[manager POST:fullurl parameters:parms constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
for (NSUInteger i= 0; i < imageNames.count; i ++) {
//上传时使用当前的系统事件作为文件名
// NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//
// formatter.dateFormat = @"yyyyMMddHHmmss";
//
// NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.png", imageNames[i]];
//服务器上传文件的字段和类型
UIImage *image =[GIGaUserFileHelper getUserFeedbackImag:imageNames[i]] ;
NSData *imgData = UIImagePNGRepresentation(image);
[formData appendPartWithFileData:imgData name:@"file" fileName:fileName mimeType:@"image/png"];
[formData appendPartWithFormData:imgData name:fileName];
}
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@", dict);
NSDictionary *headerFields = [(NSHTTPURLResponse *)task.response allHeaderFields];
if (block) {
block(dict, headerFields, nil);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSDictionary *headerFields = [(NSHTTPURLResponse *)task.response allHeaderFields];
if (block) {
block(nil, headerFields, error);
}
}];
}
@end
@@ -15,6 +15,13 @@
+ (UIImage *) scaleFromImage: (UIImage *) image toSize: (CGSize) size;
+ (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)asize;
+(void)savaShareImange:(NSString *)fileName image:(UIImage *)image;
+(UIImage *)zipScaleWithImage:(UIImage *)sourceImage;
+(UIImage *)getUserShareImage:(NSString *)imageName;
+(void)delectShareImage:(NSString *)imageName;
+(void)saveUserFeedbackImag:(NSString *)imagename image:(UIImage *)image;
+(UIImage *)getUserFeedbackImag:(NSString *)imageName;
+(void)delectAllfeedimages;
@end
@@ -20,6 +20,33 @@
}
+(void)savaShareImange:(NSString *)fileName image:(UIImage *)image{
NSData*imageData=UIImageJPEGRepresentation(image,1.0f);
NSString *fullPath=[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:fileName];
[imageData writeToFile:fullPath atomically:YES];
}
+(void)delectShareImage:(NSString *)imageName{
//删除文件夹及文件级内的文件:
NSString *fullPath=[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:imageName];
NSFileManager *fileManager = [NSFileManager defaultManager];[fileManager removeItemAtPath:fullPath error:nil];
}
+(UIImage *)getUserShareImage:(NSString *)imageName{
NSString*fullpath=[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:imageName];
UIImage*saveImage=[[UIImage alloc]initWithContentsOfFile:fullpath];
if (!saveImage) {
saveImage = [UIImage imageNamed:@"sharebg_1"];
}
return saveImage;
}
+ (UIImage *)getUserAvatorWith:(NSString *)userId{
if (!userId) {
@@ -81,4 +108,65 @@
return newimage;
}
+(UIImage *)zipScaleWithImage:(UIImage *)sourceImage{
//进行图像尺寸的压缩
CGSize imageSize = sourceImage.size;
//取出要压缩的image尺寸
CGFloat width = imageSize.width;
//图片宽度
CGFloat height = imageSize.height;
//图片高度 //1.宽高大于1280(宽高比不按照2来算,按照1来算)
if (width>1280||height>1280) { if (width>height) { CGFloat scale = height/width; width = 1280; height = width*scale;
}else{ CGFloat scale = width/height;
height = 1280; width = height*scale;
}
//2.宽大于1280高小于1280
}else if(width>1280||height<1280){
CGFloat scale = height/width;
width = 1280;
height = width*scale;
//3.宽小于1280高大于1280
}else if(width<1280||height>1280){
CGFloat scale = width/height; height = 1280; width = height*scale;
//4.宽高都小于1280
}else{
}
//进行尺寸重绘
UIGraphicsBeginImageContext(CGSizeMake(width, height));
[sourceImage drawInRect:CGRectMake(0,0,width,height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
return newImage;
}
+(void)saveUserFeedbackImag:(NSString *)imagename image:(UIImage *)image
{
NSData*imageData=UIImagePNGRepresentation(image);
NSString *fullPath=[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:[NSString stringWithFormat:@"gigafeed/%@.png",imagename]];
NSLog(@"fullPath%@",fullPath);
[imageData writeToFile:fullPath atomically:YES];
}
+(UIImage *)getUserFeedbackImag:(NSString *)imageName{
NSString*fullpath=[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:[NSString stringWithFormat:@"gigafeed/%@.png",imageName]];
UIImage*saveImage=[[UIImage alloc]initWithContentsOfFile:fullpath];
return saveImage;
}
+(void)delectAllfeedimages{
NSString *fullPath=[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:[NSString stringWithFormat:@"gigafeed/"]];
NSFileManager *fileManager = [NSFileManager defaultManager];[fileManager removeItemAtPath:fullPath error:nil];
}
@end
@@ -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
+34
View File
@@ -0,0 +1,34 @@
//
// SJPhotoPickerMacro.h
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/17.
// Copyright © 2016年 S.J. All rights reserved.
//
#ifndef SJPhotoPickerMacro_h
#define SJPhotoPickerMacro_h
#define SCREEN_W CGRectGetWidth([UIScreen mainScreen].bounds)
#define SCREEN_H CGRectGetHeight([UIScreen mainScreen].bounds)
#define Image_W (SCREEN_W - 15.0) / 4.0
#define MAXCOUNT 6
#define NAVBAR_CLOOUR [UIColor colorWithRed:131/255.0 green:10/255.0 blue:10/255.0 alpha:1]
/**
* Notification
*/
#define PICKEDARRAY_SJNOTIFY_CHANGE @"SJPhotoPickedArrayChangeNotification" // 选择图片图片数组中数据改变
#import "SJPhotoPickerManager.h"
#endif /* SJPhotoPickerMacro_h */
@@ -0,0 +1,23 @@
//
// SJPhotoPickerManager.h
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/19.
// Copyright © 2016年 S.J. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <Photos/Photos.h>
typedef void(^ImageBlcok)(UIImage *image);
typedef void(^AlbumBlock)(NSArray *albumArray);
@interface SJPhotoPickerManager : NSObject
+ (instancetype)shareSJPhotoPickerManager;
- (void)requestImageForPHAsset:(PHAsset *)asset targetSize:(CGSize)targetSize imageResult:(ImageBlcok)imageBlock;
- (void)requestAlbumsWithType:(PHAssetCollectionType)type albumResult:(AlbumBlock)albumBlock;
@end
@@ -0,0 +1,84 @@
//
// SJPhotoPickerManager.m
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/19.
// Copyright © 2016年 S.J. All rights reserved.
//
#import "SJPhotoPickerManager.h"
#import "SJAlbumModel.h"
@interface SJPhotoPickerManager()
@end
@implementation SJPhotoPickerManager
+ (instancetype)shareSJPhotoPickerManager {
static SJPhotoPickerManager *manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (!manager) {
manager = [[SJPhotoPickerManager alloc] init];
}
});
return manager;
}
- (void)requestImageForPHAsset:(PHAsset *)asset targetSize:(CGSize)targetSize imageResult:(ImageBlcok)imageBlock {
// 使用PHImageManager从PHAsset中请求图片
PHImageManager *imageManager = [[PHImageManager alloc] init];
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.resizeMode = PHImageRequestOptionsResizeModeExact;
options.networkAccessAllowed = YES;
[imageManager requestImageForAsset:asset targetSize:targetSize contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
if (result) {
imageBlock(result);
}
}];
}
- (void)requestAlbumsWithType:(PHAssetCollectionType)type albumResult:(AlbumBlock)albumBlock {
// 列出所有相册智能相册
PHFetchResult *assetCollectionResult = [PHAssetCollection fetchAssetCollectionsWithType:type subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
NSMutableArray *albumArray = [NSMutableArray array];
// 这时 smartAlbums 中保存的应该是各个智能相册对应的 PHAssetCollection
for (NSInteger i = 0; i < assetCollectionResult.count; i++) {
// 获取一个相册(PHAssetCollection
PHCollection *collection = assetCollectionResult[i];
if ([collection class] == [PHAssetCollection class]) {
PHAssetCollection *assetCollection = (PHAssetCollection *)collection;
PHFetchResult *assetResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];
if (assetResult.count != 0) {
SJAlbumModel *model = [[SJAlbumModel alloc] init];
model.title = collection.localizedTitle;
model.assetResult = assetResult;
[albumArray addObject:model];
}
}
albumBlock(albumArray);
}
}
@end
+17
View File
@@ -0,0 +1,17 @@
//
// SJAlbumModel.h
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/22.
// Copyright © 2016年 S.J. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <Photos/Photos.h>
@interface SJAlbumModel : NSObject
@property (nonatomic, strong) PHFetchResult *assetResult;
@property (nonatomic, strong) NSString *title;
@end
+13
View File
@@ -0,0 +1,13 @@
//
// SJAlbumModel.m
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/22.
// Copyright © 2016年 S.J. All rights reserved.
//
#import "SJAlbumModel.h"
@implementation SJAlbumModel
@end
+36
View File
@@ -0,0 +1,36 @@
//
// SJPhotoModel.h
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/19.
// Copyright © 2016年 S.J. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <Photos/Photos.h>
@interface SJPhotoModel : NSObject <NSCopying>
/**
* 图片资源
*/
@property (nonatomic, strong) PHAsset *asset;
/**
* 选中的次序起始值为1 , 0表示没被选中
*/
@property (nonatomic, assign) NSInteger pickedIndex;
/**
* 在相册中次序起始值为 0
*/
@property (nonatomic, assign) NSInteger inAlbumIndex;
/**
* 是否选中 YES——选择 NO——取消选择
*/
@property (nonatomic, assign) BOOL isPicked;
@end
+25
View File
@@ -0,0 +1,25 @@
//
// SJPhotoModel.m
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/19.
// Copyright © 2016年 S.J. All rights reserved.
//
#import "SJPhotoModel.h"
@implementation SJPhotoModel
-(id)copyWithZone:(NSZone *)zone {
SJPhotoModel *modelCopy = [[[self class] allocWithZone:zone] init];
modelCopy.asset = self.asset;
modelCopy.pickedIndex = self.pickedIndex;
modelCopy.inAlbumIndex = self.inAlbumIndex;
return modelCopy;
}
@end
+24
View File
@@ -0,0 +1,24 @@
//
// SJPhotoPicker.h
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/23.
// Copyright © 2016年 S.J. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "SJPhotoPickerManager.h"
typedef void(^SJPhotoPickerBlock)(NSArray <PHAsset *> *assets);
@interface SJPhotoPicker : NSObject
@property (nonatomic, strong) SJPhotoPickerBlock photoPickerBlock;
+ (instancetype)shareSJPhotoPicker;
- (void) showPhotoPickerToController:(UIViewController *)controller pickedAssets:(SJPhotoPickerBlock)photoPickerBlcok;
@end
+71
View File
@@ -0,0 +1,71 @@
//
// SJPhotoPicker.m
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/23.
// Copyright © 2016年 S.J. All rights reserved.
//
#import "SJPhotoPicker.h"
#import "SJPhotoPickerNavController.h"
@implementation SJPhotoPicker
+ (instancetype)shareSJPhotoPicker {
static SJPhotoPicker *photoPicker = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (!photoPicker) {
photoPicker = [[SJPhotoPicker alloc] init];
}
});
return photoPicker;
}
- (void) showPhotoPickerToController:(UIViewController *)controller pickedAssets:(SJPhotoPickerBlock)photoPickerBlcok {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status){
dispatch_async(dispatch_get_main_queue(), ^{
switch (status) {
case PHAuthorizationStatusNotDetermined:
{
NSLog(@"用户还没有做出选择");
break;
}
case PHAuthorizationStatusAuthorized:
{
NSLog(@"用户允许当前应用访问相册");
SJPhotoPickerNavController *vc = [[SJPhotoPickerNavController alloc] init];
[controller presentViewController:vc animated:YES completion:nil];
self.photoPickerBlock = photoPickerBlcok;
break;
}
case PHAuthorizationStatusDenied:
{
NSLog(@"用户拒绝当前应用访问相册,我们需要提醒用户打开访问开关");
break;
}
case PHAuthorizationStatusRestricted:
{
NSLog(@"家长控制,不允许访问");
break;
}
default:
{
NSLog(@"default");
break;
}
}
});
}];
}
@end
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

+16
View File
@@ -0,0 +1,16 @@
//
// SJPhotoAlbumCell.h
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/22.
// Copyright © 2016年 S.J. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface SJPhotoAlbumCell : UITableViewCell
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) UIImage *img;
@end
+102
View File
@@ -0,0 +1,102 @@
//
// SJPhotoAlbumCell.m
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/22.
// Copyright © 2016年 S.J. All rights reserved.
//
#import "SJPhotoAlbumCell.h"
#import "SJPhotoPickerMacro.h"
@interface SJPhotoAlbumCell()
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *underLine;
@end
@implementation SJPhotoAlbumCell
- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self s_setupCellView];
}
return self;
}
- (void)s_setupCellView {
CGFloat X_Padding = 15.0;
self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(X_Padding, 0, 55, 54)];
self.imgView.backgroundColor= [UIColor blueColor];
[self.contentView addSubview:self.imgView];
self.titleLab = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.imgView.frame) + 15 , 0,SCREEN_W - CGRectGetWidth(self.imgView.frame) - 15 * 2 - 30, 54)];
[self.contentView addSubview:self.titleLab];
self.underLine = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.imgView.frame) + 15 , CGRectGetMaxY(self.titleLab.frame),SCREEN_W - CGRectGetWidth(self.imgView.frame) - 15 * 2, 1)];
self.underLine.backgroundColor = [UIColor colorWithRed:236/255.0 green:236/255.0 blue:236/255.0 alpha:1];
[self.contentView addSubview:self.underLine];
}
- (NSString *)title {
return self.title;
}
- (void)setTitle:(NSString *)title {
self.titleLab.attributedText = [self attributedString:title];
}
- (UIImage *)img {
return self.img;
}
- (void)setImg:(UIImage *)img {
self.imgView.image = img;
}
/**
* 更改字符串的前两个空格之间字符串的颜色
*
* @param txt 要改变颜色的字符床
*
* @return 改变后的字符串
*/
- (NSMutableAttributedString *)attributedString:(NSString *)txt {
// 查找出前两个空格的位置
NSInteger start = 0;
NSInteger end = 0;
NSRange countRange = NSMakeRange(0, 0);
NSString *leftString = @"";
int leftAsciiCode = [leftString characterAtIndex:0]; // 65
NSString *rightString = @"";
int rightAsciiCode = [rightString characterAtIndex:0]; // 65
for (int i = 0 ; i < txt.length; i ++) {
if ([txt characterAtIndex:i] == leftAsciiCode) {
start = i;
}
if([txt characterAtIndex:i] == rightAsciiCode){
end = i;
}
countRange = NSMakeRange(start, end - start + 1);
}
// 改变范围内字符颜色
NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:txt];
[attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:countRange];
return attributeStr;
}
@end
@@ -0,0 +1,30 @@
//
// SJPhotoPickerNavBar.h
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/22.
// Copyright © 2016年 S.J. All rights reserved.
//
#import <UIKit/UIKit.h>
@protocol SJPhotoPickerNavBarDelegate <NSObject>
@optional
- (void)leftBtnAction:(UIButton *)sender;
- (void)rightBtnAction:(UIButton *)sender;
@end
@interface SJPhotoPickerNavBar : UIView
@property (nonatomic, strong) id delegate;
@property (nonatomic, assign) NSInteger rightBtnTitleIndex;
@property (nonatomic, strong) NSString *barTitle;
@property (nonatomic, strong) NSString *leftBtnTitle;
- (void)changeHideState;
@end
+145
View File
@@ -0,0 +1,145 @@
//
// SJPhotoPickerNavBar.m
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/22.
// Copyright © 2016年 S.J. All rights reserved.
//
#import "SJPhotoPickerNavBar.h"
#import "SJPhotoPickerMacro.h"
#import "SJPhotoPickerRaidoButton.h"
@interface SJPhotoPickerNavBar()
@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) SJPhotoPickerRaidoButton *rightBtn;
@property (nonatomic, assign) BOOL barHide;
@end
@implementation SJPhotoPickerNavBar
- (instancetype)init {
self = [super initWithFrame:CGRectMake(0, 0, SCREEN_W, 64)];
if (self) {
[self setupViews];
}
return self;
}
- (void)setupViews {
self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
self.barHide = NO;
// leftBtn
{
self.leftBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
self.leftBtn.frame = CGRectMake(0, 20, 70, 44);
[self.leftBtn setTitleColor:[UIColor colorWithWhite:0.5 alpha:0.5] forState:(UIControlStateHighlighted)];
[self.leftBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 0)];
[self.leftBtn setImage:[UIImage imageNamed:@"photopicker_back"] forState:(UIControlStateNormal)];
[self.leftBtn addTarget:self action:@selector(leftBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
[self addSubview:self.leftBtn];
}
// titleLab
{
self.titleLab = [[UILabel alloc] init];
self.titleLab.font = [UIFont systemFontOfSize:16];
self.titleLab.textColor = [UIColor whiteColor];
self.titleLab.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.titleLab];
}
// rightBtn
{
self.rightBtn = [[SJPhotoPickerRaidoButton alloc] initWithFrame:CGRectMake(SCREEN_W - 35, 31, 20, 20)];
[self.rightBtn addTarget:self action:@selector(rightBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
[self addSubview:self.rightBtn];
}
}
- (void)setRightBtnTitleIndex:(NSInteger)rightBtnTitleIndex {
_rightBtnTitleIndex = rightBtnTitleIndex;
[self.rightBtn setPickedIndex:rightBtnTitleIndex];
}
- (void)setLeftBtnTitle:(NSString *)leftBtnTitle {
_leftBtnTitle = leftBtnTitle;
CGFloat titleWidth = [self widthWithString:leftBtnTitle textFontSize:16];
self.leftBtn.frame = CGRectMake(10, 20, titleWidth + 20, 44);
[self.leftBtn setTitle:leftBtnTitle forState:(UIControlStateNormal)];
}
- (void)setBarTitle:(NSString *)barTitle {
_barTitle = barTitle;
CGFloat titleWidth = [self widthWithString:barTitle textFontSize:16];
self.titleLab.frame = CGRectMake((SCREEN_W - titleWidth) * 0.5, 20, titleWidth, 44);
self.titleLab.text = barTitle;
}
- (void)leftBtnAction:(UIButton *)sender {
UIViewController *vc = [self getCurrentViewController];
if (vc.navigationController) {
[vc.navigationController popViewControllerAnimated:YES];
}
}
- (void)rightBtnAction:(UIButton *)sender {
[self.delegate rightBtnAction:sender];
}
- (void)changeHideState {
self.barHide = !self.barHide;
if (self.barHide) {
[UIView animateWithDuration:0.3 animations:^{
self.frame = CGRectMake(0, -64, SCREEN_W, 64);
}];
}
else {
[UIView animateWithDuration:0.3 animations:^{
self.frame = CGRectMake(0, 0, SCREEN_W, 64);
}];
}
}
/** 获取当前View的控制器对象 */
-(UIViewController *)getCurrentViewController{
UIResponder *next = [self nextResponder];
do {
if ([next isKindOfClass:[UIViewController class]]) {
return (UIViewController *)next;
}
next = [next nextResponder];
} while (next != nil);
return nil;
}
- (CGFloat)widthWithString:(NSString *)text textFontSize:(CGFloat)size {
CGRect rect = [text boundingRectWithSize:CGSizeMake(LONG_MAX, 8)options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil];
return rect.size.width;
}
@end
@@ -0,0 +1,19 @@
//
// SJPhotoPickerRaidoButton.h
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/22.
// Copyright © 2016年 S.J. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef void(^RadioButtonTouchedBlock)();
@interface SJPhotoPickerRaidoButton : UIButton
@property (nonatomic, assign) NSInteger pickedIndex;
@property (nonatomic, strong) RadioButtonTouchedBlock touchedBlock;
@end
@@ -0,0 +1,45 @@
//
// SJPhotoPickerRaidoButton.m
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/22.
// Copyright © 2016年 S.J. All rights reserved.
//
#import "SJPhotoPickerRaidoButton.h"
#import "SJPhotoPickerMacro.h"
@implementation SJPhotoPickerRaidoButton
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self s_setupViews];
}
return self;
}
- (void)s_setupViews {
self.titleLabel.font = [UIFont systemFontOfSize:13];
self.layer.cornerRadius = CGRectGetWidth(self.frame) * 0.5;
self.layer.masksToBounds = YES;
self.layer.borderWidth = 1;
self.layer.borderColor = [UIColor whiteColor].CGColor;
}
- (void)setPickedIndex:(NSInteger)pickedIndex{
if (pickedIndex != 0) {
self.backgroundColor = NAVBAR_CLOOUR;
[self setTitle:[NSString stringWithFormat:@"%ld",pickedIndex] forState:(UIControlStateNormal)];
}
else {
self.backgroundColor = [UIColor colorWithRed:148/255.0 green:148/255.0 blue:148/255.0 alpha:0.6];
[self setTitle:@"" forState:(UIControlStateNormal)];
}
}
@end
@@ -0,0 +1,22 @@
//
// SJPhotoPickerToolBar.h
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/22.
// Copyright © 2016年 S.J. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "SJPhotoPickerMacro.h"
@interface SJPhotoPickerToolBar : UIView
@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UIButton *rightBtn;
- (void)changePickedIndex:(NSInteger) pickedIndex;
- (void)changeHideState;
@end
@@ -0,0 +1,99 @@
//
// SJPhotoPickerToolBar.m
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/22.
// Copyright © 2016年 S.J. All rights reserved.
//
#import "SJPhotoPickerToolBar.h"
#import "SJPhotoPickerMacro.h"
@interface SJPhotoPickerToolBar()
@property (nonatomic, assign) BOOL barHide;
@end;
@implementation SJPhotoPickerToolBar
- (instancetype)init {
self = [super init];
if (self) {
self.frame = CGRectMake(0, SCREEN_H - 44, SCREEN_W, 44);
[self s_setupViews];
}
return self;
}
- (void)s_setupViews {
self.layer.shadowOffset = CGSizeMake(0, -0.3);
self.layer.shadowColor= [UIColor lightGrayColor].CGColor;
self.layer.shadowRadius = 1;
self.layer.shadowOpacity = 1;
self.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:0.9];
self.leftBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
self.leftBtn.frame = CGRectMake(5, 7, 60, 30);
[self.leftBtn setTitleColor:NAVBAR_CLOOUR forState:(UIControlStateNormal)];
self.leftBtn.titleLabel.font = [UIFont systemFontOfSize:16];
[self.leftBtn setTitle:@"预览" forState:(UIControlStateNormal)];
[self addSubview:self.leftBtn];
self.rightBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
self.rightBtn.frame = CGRectMake(SCREEN_W - 75, 7, 65, 30);
self.rightBtn.backgroundColor = NAVBAR_CLOOUR;
[self.rightBtn setTitleColor:[UIColor colorWithRed:195/255.0 green:195/255.0 blue:195/255.0 alpha:1] forState:(UIControlStateNormal)];
self.rightBtn.layer.cornerRadius = 2;
self.rightBtn.layer.masksToBounds = YES;
self.rightBtn.titleLabel.font = [UIFont systemFontOfSize:14];
[self.rightBtn setTitle:@"确定" forState:(UIControlStateNormal)];
[self addSubview:self.rightBtn];
self.barHide = NO;
}
- (void)changePickedIndex:(NSInteger) pickedIndex {
if (pickedIndex == 0) {
[self.leftBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
self.rightBtn.backgroundColor = NAVBAR_CLOOUR;
self.leftBtn.backgroundColor = NAVBAR_CLOOUR;
[self.rightBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
[self.rightBtn setTitle:@"确定" forState:(UIControlStateNormal)];
}
else {
self.rightBtn.backgroundColor = NAVBAR_CLOOUR;
self.leftBtn.backgroundColor = NAVBAR_CLOOUR;
[self.rightBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
[self.rightBtn setTitle:[NSString stringWithFormat:@"确定(%lu)",(unsigned long)pickedIndex] forState:(UIControlStateNormal)];
[self.leftBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
}
}
- (void)changeHideState {
self.barHide = !self.barHide;
if (self.barHide) {
[UIView animateWithDuration:0.3 animations:^{
self.frame = CGRectMake(0, SCREEN_H, SCREEN_W, 44);
}];
}
else {
[UIView animateWithDuration:0.3 animations:^{
self.frame = CGRectMake(0, SCREEN_H - 44, SCREEN_W, 44);
}];
}
}
@end
+27
View File
@@ -0,0 +1,27 @@
//
// SJPickPhotoCell.h
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/19.
// Copyright © 2016年 S.J. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <Photos/Photos.h>
#import "SJPhotoPickerMacro.h"
#import "SJPhotoModel.h"
typedef void(^PickBtnBlock)(SJPhotoModel *curPickModel);
typedef void(^ModelChangeBlock)(SJPhotoModel *modifiedModel);
@interface SJPickPhotoCell : UICollectionViewCell
@property (nonatomic, strong) SJPhotoModel *model;
@property (nonatomic, strong) PickBtnBlock pickBtnBlock;
- (void)modelChangePickedIndex:(NSInteger)pickedIndex;
@end
+125
View File
@@ -0,0 +1,125 @@
//
// SJPickPhotoCell.m
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/19.
// Copyright © 2016年 S.J. All rights reserved.
//
#import "SJPickPhotoCell.h"
#import "SJPhotoPickerManager.h"
#import "SJPhotoPickerMacro.h"
@interface SJPickPhotoCell()
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UIButton *pickBtn;
@end
@implementation SJPickPhotoCell
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setupViews];
}
return self;
}
- (void)setupViews {
// imgView
{
self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,Image_W, Image_W)];
self.imgView.contentMode = UIViewContentModeScaleAspectFit;
self.imgView.backgroundColor = NAVBAR_CLOOUR;
[self.contentView addSubview:self.imgView];
}
// pickBtn
{
self.pickBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
self.pickBtn.frame = CGRectMake(CGRectGetMaxX(self.imgView.frame) - CGRectGetWidth(self.imgView.frame) * 0.35,CGRectGetWidth(self.imgView.frame) * 0.05, CGRectGetWidth(self.imgView.frame) * 0.3, CGRectGetWidth(self.imgView.frame) * 0.3);
self.pickBtn.layer.cornerRadius = CGRectGetWidth(self.pickBtn.frame) * 0.5;
self.pickBtn.layer.masksToBounds = YES;
self.pickBtn.layer.borderWidth = 1;
self.pickBtn.layer.borderColor = [UIColor whiteColor].CGColor;
self.pickBtn.titleLabel.font = [UIFont systemFontOfSize:13];
self.pickBtn.backgroundColor = NAVBAR_CLOOUR;
[self.pickBtn addTarget:self action:@selector(pickBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
[self.contentView addSubview:self.pickBtn];
}
}
- (void)setModel:(SJPhotoModel *)model {
_model = model;
[[SJPhotoPickerManager shareSJPhotoPickerManager] requestImageForPHAsset:model.asset targetSize:CGSizeMake(Image_W, Image_W) imageResult:^(UIImage *image) {
if (image) {
self.imgView.image = image;
}
}];
if (model.pickedIndex != 0) {
[self.pickBtn setTitle:[NSString stringWithFormat:@"%ld",(long)model.pickedIndex] forState:(UIControlStateNormal)];
self.pickBtn.backgroundColor = NAVBAR_CLOOUR;
}
else {
[self.pickBtn setTitle:@"" forState:(UIControlStateNormal)];
self.pickBtn.backgroundColor = [UIColor colorWithRed:148/255.0 green:148/255.0 blue:148/255.0 alpha:0.6];
}
}
- (void)modelChangePickedIndex:(NSInteger)pickedIndex{
if (pickedIndex != 0) {
self.model.pickedIndex = pickedIndex;
self.model.isPicked = YES;
[self.pickBtn setTitle:[NSString stringWithFormat:@"%ld",(long)pickedIndex] forState:(UIControlStateNormal)];
self.pickBtn.backgroundColor = NAVBAR_CLOOUR;
[self showOscillatoryAnimationWithLayer:self.pickBtn.layer];
}
else {
self.model.pickedIndex = 0;
self.model.isPicked = NO;
[self.pickBtn setTitle:@"" forState:(UIControlStateNormal)];
self.pickBtn.backgroundColor = [UIColor colorWithRed:148/255.0 green:148/255.0 blue:148/255.0 alpha:0.6];
}
}
- (void)showOscillatoryAnimationWithLayer:(CALayer *)layer {
NSNumber *animationScale1 = @(1.15);
NSNumber *animationScale2 = @(0.85);
NSNumber *animationScale3 = @(1.0);
[UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
[layer setValue:animationScale1 forKeyPath:@"transform.scale"];
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
[layer setValue:animationScale2 forKeyPath:@"transform.scale"];
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
[layer setValue:animationScale3 forKeyPath:@"transform.scale"];
} completion:nil];
}];
}];
}
- (void)pickBtnAction:(UIButton *)sender {
self.model.isPicked = !self.model.isPicked;
self.pickBtnBlock(self.model);
}
@end
+21
View File
@@ -0,0 +1,21 @@
//
// SJPreviewPhotoCell.h
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/19.
// Copyright © 2016年 S.J. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <Photos/Photos.h>
typedef void(^SingleTapEventBlock)(void);
@interface SJPreviewPhotoCell : UICollectionViewCell
@property (nonatomic, strong) PHAsset *asset;
@property (nonatomic, assign) CGFloat zoomScale;
@property (nonatomic, strong) SingleTapEventBlock singleTapEvent;
@end
+127
View File
@@ -0,0 +1,127 @@
//
// SJPreviewPhotoCell.m
// SJPhotoPickerDemo
//
// Created by Jaesun on 16/8/19.
// Copyright © 2016年 S.J. All rights reserved.
//
#import "SJPreviewPhotoCell.h"
#import "SJPhotoPickerMacro.h"
@interface SJPreviewPhotoCell() <UIScrollViewDelegate>
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UIScrollView *scrollView;
@end
@implementation SJPreviewPhotoCell
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setupViews];
}
return self;
}
- (void)setupViews {
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
self.scrollView.delegate = self;
self.scrollView.maximumZoomScale = 4.0;
self.scrollView.minimumZoomScale = 1.0;
self.scrollView.contentSize = CGSizeMake(SCREEN_W, SCREEN_H);
[self.contentView addSubview:self.scrollView];
self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
self.imgView.contentMode = UIViewContentModeScaleAspectFit;
[self.scrollView addSubview:self.imgView];
UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureAction:)];
[self.scrollView addGestureRecognizer:singleTapGesture];
UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapGestureAction:)];
[doubleTapGesture setNumberOfTapsRequired:2]; // Default is 1
[self.scrollView addGestureRecognizer:doubleTapGesture];
// 如果满足双击条件,单击事件触发失败,防止双击时单击事件同时被触发
[singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];
}
- (void)setAsset:(PHAsset *)asset {
_asset = asset;
[[SJPhotoPickerManager shareSJPhotoPickerManager] requestImageForPHAsset:asset targetSize:PHImageManagerMaximumSize imageResult:^(UIImage *image) {
if (image) {
self.imgView.image = image;
CGFloat scale_H = SCREEN_H / image.size.height;
CGFloat scale_W = SCREEN_W / image.size.width;
CGSize size;
if (scale_H > scale_W) {
size = CGSizeMake(SCREEN_W, scale_W * image.size.height);
}
else if (scale_H < scale_W) {
size = CGSizeMake(scale_H * image.size.width, SCREEN_H);
}
self.imgView.frame = CGRectMake(0, 0, size.width, size.height);
self.imgView.center = self.scrollView.center;
}
}];
}
- (void)setZoomScale:(CGFloat)zoomScale {
_zoomScale = zoomScale;
[UIView animateWithDuration:0.3 animations:^{
self.scrollView.zoomScale = zoomScale;
}];
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
CGSize viewSize = self.imgView.frame.size;
CGPoint centerPoint;
if (viewSize.width<=CGRectGetWidth(self.bounds) && viewSize.height<=CGRectGetHeight(self.bounds)) {
centerPoint = CGPointMake(CGRectGetWidth(self.bounds)*0.5f, CGRectGetHeight(self.bounds)*0.5f);
}else if (viewSize.width>=CGRectGetWidth(self.bounds) && viewSize.height>=CGRectGetHeight(self.bounds) ){
centerPoint = CGPointMake(scrollView.contentSize.width*0.5f, scrollView.contentSize.height*0.5f);
}else if (viewSize.width<=CGRectGetWidth(self.bounds) && viewSize.height>=CGRectGetHeight(self.bounds) ){
centerPoint = CGPointMake(CGRectGetWidth(self.bounds)*0.5f, scrollView.contentSize.height*0.5f);
}else if ((viewSize.width >= CGRectGetWidth(self.bounds)) && (viewSize.height <= CGRectGetHeight(self.bounds))){
centerPoint = CGPointMake(scrollView.contentSize.width*0.5f, CGRectGetHeight(self.bounds)*0.5f);
}
self.imgView.center = centerPoint;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.imgView;
}
- (void)singleTapGestureAction:(UITapGestureRecognizer *)sender {
self.singleTapEvent();
}
- (void)doubleTapGestureAction:(UITapGestureRecognizer *)sender {
if (self.zoomScale == 4) {
self.zoomScale = 1;
}else {
CGFloat newScale = self.zoomScale * 2.0f;
self.zoomScale = newScale;
}
}
@end