Commit dbd1e7fb authored by ziy's avatar ziy

加入多选功能

parent 3b9f75b7
Pipeline #187 failed with stages
......@@ -55,7 +55,7 @@ RCT_EXPORT_METHOD(showImagePicker:(NSDictionary *)options
RCT_REMAP_METHOD(asyncShowImagePicker,
options:(NSDictionary *)options
showImagePickerResolver:(RCTPromiseResolveBlock)resolve
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
self.cameraOptions = options;
self.resolveBlock = resolve;
......@@ -72,17 +72,6 @@ RCT_EXPORT_METHOD(openCamera:(NSDictionary *)options callback:(RCTResponseSender
[self takePhoto];
}
RCT_REMAP_METHOD(asyncOpenCamera,
options:(NSDictionary *)options
openCameraResolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
self.cameraOptions = options;
self.resolveBlock = resolve;
self.rejectBlock = reject;
self.callback = nil;
[self takePhoto];
}
RCT_EXPORT_METHOD(deleteCache) {
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath: [NSString stringWithFormat:@"%@ImageCaches", NSTemporaryDirectory()] error:nil];
......@@ -158,7 +147,7 @@ RCT_EXPORT_METHOD(openVideoPicker:(NSDictionary *)options callback:(RCTResponseS
}
}
__weak TZImagePickerController *weakPicker = imagePickerVc;
__block TZImagePickerController *weakPicker = imagePickerVc;
[imagePickerVc setDidFinishPickingPhotosWithInfosHandle:^(NSArray<UIImage *> *photos,NSArray *assets,BOOL isSelectOriginalPhoto,NSArray<NSDictionary *> *infos) {
NSMutableArray *selectArray = [NSMutableArray array];
for (NSInteger i = 0; i < assets.count; i++) {
......@@ -226,7 +215,7 @@ RCT_EXPORT_METHOD(openVideoPicker:(NSDictionary *)options callback:(RCTResponseS
}];
}];
__weak TZImagePickerController *weakPickerVc = imagePickerVc;
__block TZImagePickerController *weakPickerVc = imagePickerVc;
[imagePickerVc setImagePickerControllerDidCancelHandle:^{
callback(@[@"取消"]);
[weakPicker dismissViewControllerAnimated:YES completion:nil];
......@@ -283,7 +272,7 @@ RCT_EXPORT_METHOD(openVideoPicker:(NSDictionary *)options callback:(RCTResponseS
}
}
__weak TZImagePickerController *weakPicker = imagePickerVc;
__block TZImagePickerController *weakPicker = imagePickerVc;
[imagePickerVc setDidFinishPickingPhotosWithInfosHandle:^(NSArray<UIImage *> *photos,NSArray *assets,BOOL isSelectOriginalPhoto,NSArray<NSDictionary *> *infos) {
if (isRecordSelected) {
self.selectedAssets = [NSMutableArray arrayWithArray:assets];
......@@ -291,20 +280,17 @@ RCT_EXPORT_METHOD(openVideoPicker:(NSDictionary *)options callback:(RCTResponseS
NSMutableArray *selectedPhotos = [NSMutableArray array];
[weakPicker showProgressHUD];
if (imageCount == 1 && isCrop) {
//增加png保留透明度功能
[selectedPhotos addObject:[self handleImageData:photos[0] info:infos[0] quality:quality]];
[selectedPhotos addObject:[self handleImageData:photos[0] quality:quality]];
} else {
[infos enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//增加png保留透明度功能
[selectedPhotos addObject:[self handleImageData:photos[idx] info:infos[idx] quality:quality]];
[selectedPhotos addObject:[self handleImageData:photos[idx] quality:quality]];
}];
}
[self invokeSuccessWithResult:selectedPhotos];
[weakPicker hideProgressHUD];
}];
__weak TZImagePickerController *weakPickerVc = imagePickerVc;
__block TZImagePickerController *weakPickerVc = imagePickerVc;
[imagePickerVc setImagePickerControllerDidCancelHandle:^{
[self invokeError];
[weakPickerVc hideProgressHUD];
......@@ -428,34 +414,6 @@ RCT_EXPORT_METHOD(openVideoPicker:(NSDictionary *)options callback:(RCTResponseS
}
}
/**
保留png透明度的回调方法
通过文件后缀判断是否是png
*/
- (NSDictionary *)handleImageData:(UIImage *) image info:(NSDictionary *)info quality:(NSInteger)quality {
NSMutableDictionary *photo = [NSMutableDictionary dictionary];
NSString *filePath = [NSString stringWithFormat:@"%@",info[@"PHImageFileURLKey"]];
NSRange range = [[filePath lowercaseString] rangeOfString:@".png"];
BOOL isPng = range.length > 0 ? YES : NO;
NSLog(@"++%@",isPng?@"is png":@"not png");
// 建议增加配置项选择是否放弃png透明度 提高压缩率
if(isPng == NO){
return [self handleImageData:image quality:quality];
}
NSData *imageData = UIImagePNGRepresentation(image);
//无需压缩 所以不需要保存临时文件直接返回原图地址 ?????
photo[@"uri"] = filePath;
photo[@"width"] = @(image.size.width);
photo[@"height"] = @(image.size.height);
photo[@"size"] = @(imageData.length);
if ([self.cameraOptions sy_boolForKey:@"enableBase64"]) {
photo[@"base64"] = isPng?[NSString stringWithFormat:@"data:image/png;base64,%@", [imageData base64EncodedStringWithOptions:0]]:[NSString stringWithFormat:@"data:image/jpeg;base64,%@", [imageData base64EncodedStringWithOptions:0]];
}
return photo;
}
- (NSDictionary *)handleImageData:(UIImage *) image quality:(NSInteger)quality {
NSMutableDictionary *photo = [NSMutableDictionary dictionary];
NSData *imageData = UIImageJPEGRepresentation(image, quality * 1.0 / 100);
......
......@@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
049D5A092321F82800B67BCD /* NSArray+CheckOutIndex.m in Sources */ = {isa = PBXBuildFile; fileRef = 049D5A082321F82800B67BCD /* NSArray+CheckOutIndex.m */; };
23F50D671F3C041A004B3E61 /* NSBundle+TZImagePicker.m in Sources */ = {isa = PBXBuildFile; fileRef = 23F50D4B1F3C0419004B3E61 /* NSBundle+TZImagePicker.m */; };
23F50D681F3C041A004B3E61 /* TZAssetCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 23F50D4D1F3C0419004B3E61 /* TZAssetCell.m */; };
23F50D691F3C041A004B3E61 /* TZAssetModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 23F50D4F1F3C0419004B3E61 /* TZAssetModel.m */; };
......@@ -38,6 +39,8 @@
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
049D5A072321F82000B67BCD /* NSArray+CheckOutIndex.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSArray+CheckOutIndex.h"; sourceTree = "<group>"; };
049D5A082321F82800B67BCD /* NSArray+CheckOutIndex.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSArray+CheckOutIndex.m"; sourceTree = "<group>"; };
134814201AA4EA6300B7C361 /* libRNSyanImagePicker.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNSyanImagePicker.a; sourceTree = BUILT_PRODUCTS_DIR; };
23F50D4A1F3C0419004B3E61 /* NSBundle+TZImagePicker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSBundle+TZImagePicker.h"; sourceTree = "<group>"; };
23F50D4B1F3C0419004B3E61 /* NSBundle+TZImagePicker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSBundle+TZImagePicker.m"; sourceTree = "<group>"; };
......@@ -125,6 +128,8 @@
23F50D641F3C041A004B3E61 /* TZVideoPlayerController.m */,
23F50D651F3C041A004B3E61 /* UIView+Layout.h */,
23F50D661F3C041A004B3E61 /* UIView+Layout.m */,
049D5A072321F82000B67BCD /* NSArray+CheckOutIndex.h */,
049D5A082321F82800B67BCD /* NSArray+CheckOutIndex.m */,
);
path = TZImagePickerController;
sourceTree = "<group>";
......@@ -199,6 +204,7 @@
files = (
23F50D6F1F3C041A004B3E61 /* TZPhotoPickerController.m in Sources */,
23F50D691F3C041A004B3E61 /* TZAssetModel.m in Sources */,
049D5A092321F82800B67BCD /* NSArray+CheckOutIndex.m in Sources */,
23F50D701F3C041A004B3E61 /* TZPhotoPreviewCell.m in Sources */,
23F50D6C1F3C041A004B3E61 /* TZImageManager.m in Sources */,
23F50D671F3C041A004B3E61 /* NSBundle+TZImagePicker.m in Sources */,
......
//
// NSArray+CheckOutIndex.h
// NewPickImage
//
// Created by Liu on 16/7/29.
// Copyright © 2016年 Liu. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSArray (CheckOutIndex)
- (id)objectAtIndexCheck:(NSUInteger)index;
@end
//
// NSArray+CheckOutIndex.m
// NewPickImage
//
// Created by Liu on 16/7/29.
// Copyright © 2016年 Liu. All rights reserved.
//
#import "NSArray+CheckOutIndex.h"
@implementation NSArray (CheckOutIndex)
- (id)objectAtIndexCheck:(NSUInteger)index {
if (self.count <= index) {
return nil;
}
if (index < 0) {
return nil;
}
return [self objectAtIndex:index];
}
@end
......@@ -205,6 +205,9 @@
if (_selectPhotoButton == nil) {
UIButton *selectPhotoButton = [[UIButton alloc] init];
[selectPhotoButton addTarget:self action:@selector(selectPhotoButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//TODO
[selectPhotoButton addTarget:self action:@selector(selectPhotoButtonClick:) forControlEvents:UIControlEventTouchDragEnter];
[selectPhotoButton addTarget:self action:@selector(selectPhotoButtonClick:) forControlEvents:UIControlEventTouchDragInside];
[self.contentView addSubview:selectPhotoButton];
_selectPhotoButton = selectPhotoButton;
}
......
......@@ -17,7 +17,15 @@
#import "TZGifPhotoPreviewController.h"
#import "TZLocationManager.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "NSArray+CheckOutIndex.h"
#define MARGIN 5
#define COL 4
#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height
struct CollectionCellPath {
unsigned int row;
unsigned int col;
};
@interface TZPhotoPickerController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIAlertViewDelegate> {
NSMutableArray *_models;
......@@ -34,6 +42,12 @@
BOOL _showTakePhotoBtn;
CGFloat _offsetItemCount;
//add by 三儿
CGFloat _cellHeight;
struct CollectionCellPath _startPath, _endPath;
BOOL _isSelectOrUnSelect;
}
@property CGRect previousPreheatRect;
@property (nonatomic, assign) BOOL isSelectOriginalPhoto;
......@@ -42,6 +56,11 @@
@property (nonatomic, strong) UIImagePickerController *imagePickerVc;
@property (strong, nonatomic) CLLocation *location;
@property (assign, nonatomic) BOOL useCachedImage;
@property (nonatomic, strong) NSMutableArray *selectedPhotos;
@property (nonatomic, strong) NSMutableArray *tmpSelectedPhotos;
@end
static CGSize AssetGridThumbnailSize;
......@@ -93,6 +112,8 @@ static CGFloat itemMargin = 5;
_showTakePhotoBtn = _model.isCameraRoll && ((tzImagePickerVc.allowTakePicture && tzImagePickerVc.allowPickingImage) || (tzImagePickerVc.allowTakeVideo && tzImagePickerVc.allowPickingVideo));
// [self resetCachedAssets];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeStatusBarOrientationNotification:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
UIPanGestureRecognizer *pGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGes:)];
[self.view addGestureRecognizer:pGes];
}
- (void)fetchAssetModels {
......@@ -294,6 +315,8 @@ static CGFloat itemMargin = 5;
}
_collectionView.frame = CGRectMake(0, top, self.view.tz_width, collectionViewHeight);
CGFloat itemWH = (self.view.tz_width - (self.columnNumber + 1) * itemMargin) / self.columnNumber;
_cellHeight=itemWH;
_startPath.row = -1;
_layout.itemSize = CGSizeMake(itemWH, itemWH);
_layout.minimumInteritemSpacing = itemMargin;
_layout.minimumLineSpacing = itemMargin;
......@@ -569,7 +592,135 @@ static CGFloat itemMargin = 5;
};
return cell;
}
- (void)panGes:(UIPanGestureRecognizer *)ges {
CGPoint point_inCollectionView = [ges locationInView:self.collectionView];
CGPoint point_inView = [ges locationInView:self.view];
unsigned int row = (point_inCollectionView.y-5)/(_cellHeight+MARGIN);
unsigned int col = (point_inCollectionView.x)/(_cellHeight+MARGIN);
if (ges.state == UIGestureRecognizerStateBegan) {
_startPath.row = row;
_startPath.col = col;
_endPath.row = row;
_endPath.col = col;
NSInteger startIndex = _startPath.row*COL+_startPath.col;
TZAssetModel *model_item = [_models objectAtIndexCheck:startIndex];
_isSelectOrUnSelect = !model_item.isSelected;
}
if (ges.state == UIGestureRecognizerStateChanged) {
_endPath.row = row;
_endPath.col = col;
NSMutableArray *mul = [NSMutableArray array];
if (_startPath.row <= _endPath.row) {
NSInteger startIndex = _startPath.row*COL+_startPath.col;
for (NSInteger i = startIndex; i <= _endPath.row*COL+_endPath.col; i++) {
TZAssetModel *model = [_models objectAtIndexCheck:i];
model.isSelected = _isSelectOrUnSelect;
if(model!=nil)
[mul addObject:model];
}
[self tmpSelectedPhotosAddCusPhotoModels:mul];
}else if (_startPath.row > _endPath.row) {
for (NSInteger i = _endPath.row*COL+_endPath.col; i <= _startPath.row*COL+_startPath.col; i++) {
TZAssetModel *model = [_models objectAtIndexCheck:i];
model.isSelected = _isSelectOrUnSelect;
if(model!=nil)
[mul addObject:model];
}
[self tmpSelectedPhotosAddCusPhotoModels:mul];
}
}
if (ges.state == UIGestureRecognizerStateEnded) {
[self.tmpSelectedPhotos removeAllObjects];
[self setUseCachedImageAndReloadData];
[self refreshBottomToolBarStatus];
}
[self performSelector:@selector(scrollCollectionViewWithPointInView:) withObject:[NSValue valueWithCGPoint:point_inView]];
}
- (void)tmpSelectedPhotosAddCusPhotoModels:(NSMutableArray *)array {
[self.tmpSelectedPhotos removeObjectsInArray:array];
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
for (TZAssetModel *model in self.tmpSelectedPhotos) {
model.isSelected = !model.isSelected;
if(model.isSelected){
[tzImagePickerVc addSelectedModel:model];
}else{
[tzImagePickerVc removeSelectedModel:model];
}
}
self.tmpSelectedPhotos = array;
for (TZAssetModel *model in self.tmpSelectedPhotos) {
if(model.isSelected){
[tzImagePickerVc addSelectedModel:model];
}else{
[tzImagePickerVc removeSelectedModel:model];
}
}
NSMutableArray *resultArray = [NSMutableArray array];
for (TZAssetModel *model in tzImagePickerVc.selectedModels) {
if (![resultArray containsObject:model]) {
[resultArray addObject:model];
}
}
tzImagePickerVc.selectedModels = resultArray;
// if (tzImagePickerVc.selectedModels.count >= tzImagePickerVc.maxImagesCount) {
// NSString *title = [NSString stringWithFormat:[NSBundle tz_localizedStringForKey:@"Select a maximum of %zd photos"], tzImagePickerVc.maxImagesCount];
// [tzImagePickerVc showAlertWithTitle:title];
// }
[self refreshBottomToolBarStatus];
[self setUseCachedImageAndReloadData];
}
- (void)scrollCollectionViewWithPointInView:(NSValue *)pointView {
CGPoint point_inView = pointView.CGPointValue;
if (point_inView.y < 64+_cellHeight) {
CGFloat distanceY = (_cellHeight+64-point_inView.y)/1.5;
CGPoint offset = self.collectionView.contentOffset;
offset.y -= distanceY;
if (offset.y < -64) {
offset.y = -64;
}
self.collectionView.contentOffset = offset;
}else if (point_inView.y > kHeight-_cellHeight) {
CGFloat distanceY = (point_inView.y - kHeight+_cellHeight)/1.5;
CGPoint offset = self.collectionView.contentOffset;
offset.y += distanceY;
if (offset.y > self.collectionView.contentSize.height-self.collectionView.bounds.size.height) {
offset.y = self.collectionView.contentSize.height-self.collectionView.bounds.size.height;
}
self.collectionView.contentOffset = offset;
}
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
// take a photo / 去拍照
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
......
......@@ -11,6 +11,7 @@
#import "UIView+Layout.h"
#import "TZImageManager.h"
#import "TZProgressView.h"
#import "TZAssetCell.h"
#import "TZImageCropManager.h"
#import <MediaPlayer/MediaPlayer.h>
#import "TZImagePickerController.h"
......@@ -131,11 +132,17 @@
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
[self addGestureRecognizer:tap1];
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
tap2.numberOfTapsRequired = 2;
[tap1 requireGestureRecognizerToFail:tap2];
[self addGestureRecognizer:tap2];
UITapGestureRecognizer *tapx = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(selectPhotoButtonClick:)];
[self addGestureRecognizer:tapx];
[self configProgressView];
}
return self;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment