Commit f2f2364d authored by banchichen's avatar banchichen

新增sortAscendingByModificationDate属性,默认为YES,照片按修改时间升序排列,为NO则拍照按钮会在第一个,照片降序排列

parent d018bef1
No preview for this file type
...@@ -46,7 +46,7 @@ ...@@ -46,7 +46,7 @@
if ([self.representedAssetIdentifier isEqualToString:[[TZImageManager manager] getAssetIdentifier:model.asset]]) { if ([self.representedAssetIdentifier isEqualToString:[[TZImageManager manager] getAssetIdentifier:model.asset]]) {
self.imageView.image = photo; self.imageView.image = photo;
} else { } else {
NSLog(@"this cell is showing other asset"); // NSLog(@"this cell is showing other asset");
[[PHImageManager defaultManager] cancelImageRequest:self.imageRequestID]; [[PHImageManager defaultManager] cancelImageRequest:self.imageRequestID];
} }
if (!isDegraded) { if (!isDegraded) {
...@@ -55,7 +55,7 @@ ...@@ -55,7 +55,7 @@
}]; }];
if (imageRequestID && self.imageRequestID && imageRequestID != self.imageRequestID) { if (imageRequestID && self.imageRequestID && imageRequestID != self.imageRequestID) {
[[PHImageManager defaultManager] cancelImageRequest:self.imageRequestID]; [[PHImageManager defaultManager] cancelImageRequest:self.imageRequestID];
NSLog(@"cancelImageRequest %d",self.imageRequestID); // NSLog(@"cancelImageRequest %d",self.imageRequestID);
} }
self.imageRequestID = imageRequestID; self.imageRequestID = imageRequestID;
self.selectPhotoButton.selected = model.isSelected; self.selectPhotoButton.selected = model.isSelected;
......
...@@ -23,6 +23,10 @@ ...@@ -23,6 +23,10 @@
/// Default is 600px / 默认600像素宽 /// Default is 600px / 默认600像素宽
@property (nonatomic, assign) CGFloat photoPreviewMaxWidth; @property (nonatomic, assign) CGFloat photoPreviewMaxWidth;
/// Sort photos ascending by modificationDate,Default is YES
/// 对照片排序,按修改时间升序,默认是YES。如果设置为NO,最新的照片会显示在最前面,内部的拍照按钮会排在第一个
@property (nonatomic, assign) BOOL sortAscendingByModificationDate;
/// Return YES if Authorized 返回YES如果得到了授权 /// Return YES if Authorized 返回YES如果得到了授权
- (BOOL)authorizationStatusAuthorized; - (BOOL)authorizationStatusAuthorized;
......
...@@ -67,12 +67,12 @@ static CGFloat TZScreenScale; ...@@ -67,12 +67,12 @@ static CGFloat TZScreenScale;
if (!allowPickingVideo) option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage]; if (!allowPickingVideo) option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage];
if (!allowPickingImage) option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", if (!allowPickingImage) option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld",
PHAssetMediaTypeVideo]; PHAssetMediaTypeVideo];
// option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]]; // option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:self.sortAscendingByModificationDate]];
option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"modificationDate" ascending:YES]]; option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"modificationDate" ascending:self.sortAscendingByModificationDate]];
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil]; PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];
for (PHAssetCollection *collection in smartAlbums) { for (PHAssetCollection *collection in smartAlbums) {
if ([collection.localizedTitle isEqualToString:@"Camera Roll"] || [collection.localizedTitle isEqualToString:@"相机胶卷"] || [collection.localizedTitle isEqualToString:@"所有照片"]) { if ([collection.localizedTitle isEqualToString:@"Camera Roll"] || [collection.localizedTitle isEqualToString:@"相机胶卷"] || [collection.localizedTitle isEqualToString:@"所有照片"] || [collection.localizedTitle isEqualToString:@"All Photos"]) {
PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:collection options:option]; PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:collection options:option];
model = [self modelWithResult:fetchResult name:collection.localizedTitle]; model = [self modelWithResult:fetchResult name:collection.localizedTitle];
if (completion) completion(model); if (completion) completion(model);
...@@ -83,7 +83,7 @@ static CGFloat TZScreenScale; ...@@ -83,7 +83,7 @@ static CGFloat TZScreenScale;
[self.assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) { [self.assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if ([group numberOfAssets] < 1) return; if ([group numberOfAssets] < 1) return;
NSString *name = [group valueForProperty:ALAssetsGroupPropertyName]; NSString *name = [group valueForProperty:ALAssetsGroupPropertyName];
if ([name isEqualToString:@"Camera Roll"] || [name isEqualToString:@"相机胶卷"] || [name isEqualToString:@"所有照片"]) { if ([name isEqualToString:@"Camera Roll"] || [name isEqualToString:@"相机胶卷"] || [name isEqualToString:@"所有照片"] || [name isEqualToString:@"All Photos"]) {
model = [self modelWithResult:group name:name]; model = [self modelWithResult:group name:name];
if (completion) completion(model); if (completion) completion(model);
*stop = YES; *stop = YES;
...@@ -100,8 +100,8 @@ static CGFloat TZScreenScale; ...@@ -100,8 +100,8 @@ static CGFloat TZScreenScale;
if (!allowPickingImage) option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", if (!allowPickingImage) option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld",
PHAssetMediaTypeVideo]; PHAssetMediaTypeVideo];
// option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]]; // option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:self.sortAscendingByModificationDate]];
option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"modificationDate" ascending:YES]]; option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"modificationDate" ascending:self.sortAscendingByModificationDate]];
PHAssetCollectionSubtype smartAlbumSubtype = PHAssetCollectionSubtypeSmartAlbumUserLibrary | PHAssetCollectionSubtypeSmartAlbumRecentlyAdded | PHAssetCollectionSubtypeSmartAlbumVideos; PHAssetCollectionSubtype smartAlbumSubtype = PHAssetCollectionSubtypeSmartAlbumUserLibrary | PHAssetCollectionSubtypeSmartAlbumRecentlyAdded | PHAssetCollectionSubtypeSmartAlbumVideos;
// For iOS 9, We need to show ScreenShots Album && SelfPortraits Album // For iOS 9, We need to show ScreenShots Album && SelfPortraits Album
...@@ -183,7 +183,7 @@ static CGFloat TZScreenScale; ...@@ -183,7 +183,7 @@ static CGFloat TZScreenScale;
} else if (allowPickingImage) { } else if (allowPickingImage) {
[group setAssetsFilter:[ALAssetsFilter allPhotos]]; [group setAssetsFilter:[ALAssetsFilter allPhotos]];
} }
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { ALAssetsGroupEnumerationResultsBlock resultBlock = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result == nil) { if (result == nil) {
if (completion) completion(photoArr); if (completion) completion(photoArr);
} }
...@@ -202,8 +202,17 @@ static CGFloat TZScreenScale; ...@@ -202,8 +202,17 @@ static CGFloat TZScreenScale;
} else { } else {
[photoArr addObject:[TZAssetModel modelWithAsset:result type:type]]; [photoArr addObject:[TZAssetModel modelWithAsset:result type:type]];
} }
};
if (self.sortAscendingByModificationDate) {
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (resultBlock) { resultBlock(result,index,stop); }
}];
} else {
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (resultBlock) { resultBlock(result,index,stop); }
}]; }];
} }
}
} }
/// Get asset at index 获得下标为index的单个照片 /// Get asset at index 获得下标为index的单个照片
...@@ -387,9 +396,14 @@ static CGFloat TZScreenScale; ...@@ -387,9 +396,14 @@ static CGFloat TZScreenScale;
return 0; return 0;
} }
/// Get postImage / 获取封面图
- (void)getPostImageWithAlbumModel:(TZAlbumModel *)model completion:(void (^)(UIImage *))completion { - (void)getPostImageWithAlbumModel:(TZAlbumModel *)model completion:(void (^)(UIImage *))completion {
if (iOS8Later) { if (iOS8Later) {
[[TZImageManager manager] getPhotoWithAsset:[model.result lastObject] photoWidth:80 completion:^(UIImage *photo, NSDictionary *info, BOOL isDegraded) { id asset = [model.result lastObject];
if (!self.sortAscendingByModificationDate) {
asset = [model.result firstObject];
}
[[TZImageManager manager] getPhotoWithAsset:asset photoWidth:80 completion:^(UIImage *photo, NSDictionary *info, BOOL isDegraded) {
if (completion) completion(photo); if (completion) completion(photo);
}]; }];
} else { } else {
...@@ -459,6 +473,7 @@ static CGFloat TZScreenScale; ...@@ -459,6 +473,7 @@ static CGFloat TZScreenScale;
#pragma mark - Get Video #pragma mark - Get Video
/// Get Video / 获取视频
- (void)getVideoWithAsset:(id)asset completion:(void (^)(AVPlayerItem * _Nullable, NSDictionary * _Nullable))completion { - (void)getVideoWithAsset:(id)asset completion:(void (^)(AVPlayerItem * _Nullable, NSDictionary * _Nullable))completion {
if ([asset isKindOfClass:[PHAsset class]]) { if ([asset isKindOfClass:[PHAsset class]]) {
[[PHImageManager defaultManager] requestPlayerItemForVideo:asset options:nil resultHandler:^(AVPlayerItem * _Nullable playerItem, NSDictionary * _Nullable info) { [[PHImageManager defaultManager] requestPlayerItemForVideo:asset options:nil resultHandler:^(AVPlayerItem * _Nullable playerItem, NSDictionary * _Nullable info) {
...@@ -476,6 +491,7 @@ static CGFloat TZScreenScale; ...@@ -476,6 +491,7 @@ static CGFloat TZScreenScale;
#pragma mark - Export video #pragma mark - Export video
/// Export Video / 导出视频,暂仅仅支持iOS8.0+
- (void)getVideoOutputPathWithAsset:(id)asset completion:(void (^)(NSString *outputPath))completion { - (void)getVideoOutputPathWithAsset:(id)asset completion:(void (^)(NSString *outputPath))completion {
if ([asset isKindOfClass:[PHAsset class]]) { if ([asset isKindOfClass:[PHAsset class]]) {
PHVideoRequestOptions* options = [[PHVideoRequestOptions alloc] init]; PHVideoRequestOptions* options = [[PHVideoRequestOptions alloc] init];
......
...@@ -34,6 +34,10 @@ ...@@ -34,6 +34,10 @@
/// Default is 9 / 默认最大可选9张图片 /// Default is 9 / 默认最大可选9张图片
@property (nonatomic, assign) NSInteger maxImagesCount; @property (nonatomic, assign) NSInteger maxImagesCount;
/// Sort photos ascending by modificationDate,Default is YES
/// 对照片排序,按修改时间升序,默认是YES。如果设置为NO,最新的照片会显示在最前面,内部的拍照按钮会排在第一个
@property (nonatomic, assign) BOOL sortAscendingByModificationDate;
/// Default is 828px / 默认828像素宽 /// Default is 828px / 默认828像素宽
@property (nonatomic, assign) CGFloat photoWidth; @property (nonatomic, assign) CGFloat photoWidth;
......
...@@ -89,6 +89,7 @@ ...@@ -89,6 +89,7 @@
self.timeout = 15; self.timeout = 15;
self.photoWidth = 828.0; self.photoWidth = 828.0;
self.photoPreviewMaxWidth = 600; self.photoPreviewMaxWidth = 600;
self.sortAscendingByModificationDate = YES;
if (![[TZImageManager manager] authorizationStatusAuthorized]) { if (![[TZImageManager manager] authorizationStatusAuthorized]) {
_tipLable = [[UILabel alloc] init]; _tipLable = [[UILabel alloc] init];
...@@ -148,6 +149,7 @@ ...@@ -148,6 +149,7 @@
_pushToPhotoPickerVc = YES; _pushToPhotoPickerVc = YES;
if (_pushToPhotoPickerVc) { if (_pushToPhotoPickerVc) {
TZPhotoPickerController *photoPickerVc = [[TZPhotoPickerController alloc] init]; TZPhotoPickerController *photoPickerVc = [[TZPhotoPickerController alloc] init];
photoPickerVc.isFirstAppear = YES;
[[TZImageManager manager] getCameraRollAlbum:self.allowPickingVideo allowPickingImage:self.allowPickingImage completion:^(TZAlbumModel *model) { [[TZImageManager manager] getCameraRollAlbum:self.allowPickingVideo allowPickingImage:self.allowPickingImage completion:^(TZAlbumModel *model) {
photoPickerVc.model = model; photoPickerVc.model = model;
[self pushViewController:photoPickerVc animated:YES]; [self pushViewController:photoPickerVc animated:YES];
...@@ -251,6 +253,11 @@ ...@@ -251,6 +253,11 @@
[[NSUserDefaults standardUserDefaults] synchronize]; [[NSUserDefaults standardUserDefaults] synchronize];
} }
- (void)setSortAscendingByModificationDate:(BOOL)sortAscendingByModificationDate {
_sortAscendingByModificationDate = sortAscendingByModificationDate;
[TZImageManager manager].sortAscendingByModificationDate = sortAscendingByModificationDate;
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (iOS7Later) viewController.automaticallyAdjustsScrollViewInsets = NO; if (iOS7Later) viewController.automaticallyAdjustsScrollViewInsets = NO;
if (_timer) { [_timer invalidate]; _timer = nil;} if (_timer) { [_timer invalidate]; _timer = nil;}
......
...@@ -11,7 +11,9 @@ ...@@ -11,7 +11,9 @@
@class TZAlbumModel; @class TZAlbumModel;
@interface TZPhotoPickerController : UIViewController @interface TZPhotoPickerController : UIViewController
@property (nonatomic, assign) BOOL isFirstAppear;
@property (nonatomic, strong) TZAlbumModel *model; @property (nonatomic, strong) TZAlbumModel *model;
@property (nonatomic, copy) void (^backButtonClickHandle)(TZAlbumModel *model); @property (nonatomic, copy) void (^backButtonClickHandle)(TZAlbumModel *model);
@end @end
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
UILabel *_originalPhotoLable; UILabel *_originalPhotoLable;
BOOL _shouldScrollToBottom; BOOL _shouldScrollToBottom;
BOOL _showTakePhotoBtn;
} }
@property CGRect previousPreheatRect; @property CGRect previousPreheatRect;
@property (nonatomic, assign) BOOL isSelectOriginalPhoto; @property (nonatomic, assign) BOOL isSelectOriginalPhoto;
...@@ -66,7 +67,15 @@ static CGSize AssetGridThumbnailSize; ...@@ -66,7 +67,15 @@ static CGSize AssetGridThumbnailSize;
self.view.backgroundColor = [UIColor whiteColor]; self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = _model.name; self.navigationItem.title = _model.name;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
if ([_model.name isEqualToString:@"相机胶卷"] || [_model.name isEqualToString:@"Camera Roll"] || [_model.name isEqualToString:@"所有照片"] || !iOS8Later) { _showTakePhotoBtn = ([_model.name isEqualToString:@"相机胶卷"] || [_model.name isEqualToString:@"Camera Roll"] || [_model.name isEqualToString:@"所有照片"] || [_model.name isEqualToString:@"All Photos"]);
if (!tzImagePickerVc.sortAscendingByModificationDate && _isFirstAppear && iOS8Later) {
[[TZImageManager manager] getCameraRollAlbum:tzImagePickerVc.allowPickingVideo allowPickingImage:tzImagePickerVc.allowPickingImage completion:^(TZAlbumModel *model) {
_model = model;
_models = [NSMutableArray arrayWithArray:_model.models];
[self initSubviews];
}];
} else {
if (_showTakePhotoBtn || !iOS8Later) {
[[TZImageManager manager] getAssetsFromFetchResult:_model.result allowPickingVideo:tzImagePickerVc.allowPickingVideo allowPickingImage:tzImagePickerVc.allowPickingImage completion:^(NSArray<TZAssetModel *> *models) { [[TZImageManager manager] getAssetsFromFetchResult:_model.result allowPickingVideo:tzImagePickerVc.allowPickingVideo allowPickingImage:tzImagePickerVc.allowPickingImage completion:^(NSArray<TZAssetModel *> *models) {
_models = [NSMutableArray arrayWithArray:models]; _models = [NSMutableArray arrayWithArray:models];
[self initSubviews]; [self initSubviews];
...@@ -75,6 +84,7 @@ static CGSize AssetGridThumbnailSize; ...@@ -75,6 +84,7 @@ static CGSize AssetGridThumbnailSize;
_models = [NSMutableArray arrayWithArray:_model.models]; _models = [NSMutableArray arrayWithArray:_model.models];
[self initSubviews]; [self initSubviews];
} }
}
// [self resetCachedAssets]; // [self resetCachedAssets];
} }
...@@ -111,7 +121,7 @@ static CGSize AssetGridThumbnailSize; ...@@ -111,7 +121,7 @@ static CGSize AssetGridThumbnailSize;
_collectionView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, -2); _collectionView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, -2);
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController; TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
if (([_model.name isEqualToString:@"相机胶卷"] || [_model.name isEqualToString:@"Camera Roll"] || [_model.name isEqualToString:@"所有照片"]) && tzImagePickerVc.allowTakePicture ) { if (_showTakePhotoBtn && tzImagePickerVc.allowTakePicture ) {
_collectionView.contentSize = CGSizeMake(self.view.tz_width, ((_model.count + 4) / 4) * self.view.tz_width); _collectionView.contentSize = CGSizeMake(self.view.tz_width, ((_model.count + 4) / 4) * self.view.tz_width);
} else { } else {
_collectionView.contentSize = CGSizeMake(self.view.tz_width, ((_model.count + 3) / 4) * self.view.tz_width); _collectionView.contentSize = CGSizeMake(self.view.tz_width, ((_model.count + 3) / 4) * self.view.tz_width);
...@@ -258,7 +268,7 @@ static CGSize AssetGridThumbnailSize; ...@@ -258,7 +268,7 @@ static CGSize AssetGridThumbnailSize;
[[TZImageManager manager] getPhotoWithAsset:model.asset completion:^(UIImage *photo, NSDictionary *info, BOOL isDegraded) { [[TZImageManager manager] getPhotoWithAsset:model.asset completion:^(UIImage *photo, NSDictionary *info, BOOL isDegraded) {
if (isDegraded) return; if (isDegraded) return;
if (photo) { if (photo) {
photo = [self scaleImage:photo toSize:CGSizeMake(tzImagePickerVc.photoWidth, tzImagePickerVc.photoWidth * photo.size.height / photo.size.width)]; photo = [self scaleImage:photo toSize:CGSizeMake(tzImagePickerVc.photoWidth, (int)(tzImagePickerVc.photoWidth * photo.size.height / photo.size.width))];
[photos replaceObjectAtIndex:i withObject:photo]; [photos replaceObjectAtIndex:i withObject:photo];
} }
if (info) [infoArr replaceObjectAtIndex:i withObject:info]; if (info) [infoArr replaceObjectAtIndex:i withObject:info];
...@@ -287,7 +297,7 @@ static CGSize AssetGridThumbnailSize; ...@@ -287,7 +297,7 @@ static CGSize AssetGridThumbnailSize;
#pragma mark - UICollectionViewDataSource && Delegate #pragma mark - UICollectionViewDataSource && Delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if ([_model.name isEqualToString:@"相机胶卷"] || [_model.name isEqualToString:@"Camera Roll"] || [_model.name isEqualToString:@"所有照片"] ) { if (_showTakePhotoBtn) {
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController; TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
if (tzImagePickerVc.allowPickingImage && tzImagePickerVc.allowTakePicture) { if (tzImagePickerVc.allowPickingImage && tzImagePickerVc.allowTakePicture) {
return _models.count + 1; return _models.count + 1;
...@@ -298,14 +308,20 @@ static CGSize AssetGridThumbnailSize; ...@@ -298,14 +308,20 @@ static CGSize AssetGridThumbnailSize;
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
// the cell lead to take a picture / 去拍照的cell // the cell lead to take a picture / 去拍照的cell
if (indexPath.row >= _models.count) { TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
if (((tzImagePickerVc.sortAscendingByModificationDate && indexPath.row >= _models.count) || (!tzImagePickerVc.sortAscendingByModificationDate && indexPath.row == 0)) && _showTakePhotoBtn) {
TZAssetCameraCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZAssetCameraCell" forIndexPath:indexPath]; TZAssetCameraCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZAssetCameraCell" forIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamedFromMyBundle:@"takePicture.png"]; cell.imageView.image = [UIImage imageNamedFromMyBundle:@"takePicture.png"];
return cell; return cell;
} }
// the cell dipaly photo or video / 展示照片或视频的cell // the cell dipaly photo or video / 展示照片或视频的cell
TZAssetCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZAssetCell" forIndexPath:indexPath]; TZAssetCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZAssetCell" forIndexPath:indexPath];
TZAssetModel *model = _models[indexPath.row]; TZAssetModel *model;
if (tzImagePickerVc.sortAscendingByModificationDate || !_showTakePhotoBtn) {
model = _models[indexPath.row];
} else {
model = _models[indexPath.row - 1];
}
cell.model = model; cell.model = model;
__weak typeof(cell) weakCell = cell; __weak typeof(cell) weakCell = cell;
...@@ -319,7 +335,7 @@ static CGSize AssetGridThumbnailSize; ...@@ -319,7 +335,7 @@ static CGSize AssetGridThumbnailSize;
model.isSelected = NO; model.isSelected = NO;
NSArray *selectedModels = [NSArray arrayWithArray:tzImagePickerVc.selectedModels]; NSArray *selectedModels = [NSArray arrayWithArray:tzImagePickerVc.selectedModels];
for (TZAssetModel *model_item in selectedModels) { for (TZAssetModel *model_item in selectedModels) {
if ([model.asset isEqual:model_item.asset]) { if ([[[TZImageManager manager] getAssetIdentifier:model.asset] isEqualToString:[[TZImageManager manager] getAssetIdentifier:model_item.asset]]) {
[tzImagePickerVc.selectedModels removeObject:model_item]; [tzImagePickerVc.selectedModels removeObject:model_item];
} }
} }
...@@ -342,12 +358,16 @@ static CGSize AssetGridThumbnailSize; ...@@ -342,12 +358,16 @@ static CGSize AssetGridThumbnailSize;
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
// take a picture / 去拍照 // take a picture / 去拍照
if (indexPath.row >= _models.count) { TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
if (((tzImagePickerVc.sortAscendingByModificationDate && indexPath.row >= _models.count) || (!tzImagePickerVc.sortAscendingByModificationDate && indexPath.row == 0)) && _showTakePhotoBtn) {
[self takePicture]; return; [self takePicture]; return;
} }
// preview phote or video / 预览照片或视频 // preview phote or video / 预览照片或视频
TZAssetModel *model = _models[indexPath.row]; NSInteger index = indexPath.row;
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController; if (!tzImagePickerVc.sortAscendingByModificationDate && _showTakePhotoBtn) {
index = indexPath.row - 1;
}
TZAssetModel *model = _models[index];
if (model.type == TZAssetModelMediaTypeVideo) { if (model.type == TZAssetModelMediaTypeVideo) {
if (tzImagePickerVc.selectedModels.count > 0) { if (tzImagePickerVc.selectedModels.count > 0) {
TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController; TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController;
...@@ -359,7 +379,7 @@ static CGSize AssetGridThumbnailSize; ...@@ -359,7 +379,7 @@ static CGSize AssetGridThumbnailSize;
} }
} else { } else {
TZPhotoPreviewController *photoPreviewVc = [[TZPhotoPreviewController alloc] init]; TZPhotoPreviewController *photoPreviewVc = [[TZPhotoPreviewController alloc] init];
photoPreviewVc.currentIndex = indexPath.row; photoPreviewVc.currentIndex = index;
photoPreviewVc.models = _models; photoPreviewVc.models = _models;
[self pushPhotoPrevireViewController:photoPreviewVc]; [self pushPhotoPrevireViewController:photoPreviewVc];
} }
...@@ -409,7 +429,6 @@ static CGSize AssetGridThumbnailSize; ...@@ -409,7 +429,6 @@ static CGSize AssetGridThumbnailSize;
photoPreviewVc.isSelectOriginalPhoto = _isSelectOriginalPhoto; photoPreviewVc.isSelectOriginalPhoto = _isSelectOriginalPhoto;
photoPreviewVc.backButtonClickBlock = ^(BOOL isSelectOriginalPhoto) { photoPreviewVc.backButtonClickBlock = ^(BOOL isSelectOriginalPhoto) {
weakSelf.isSelectOriginalPhoto = isSelectOriginalPhoto; weakSelf.isSelectOriginalPhoto = isSelectOriginalPhoto;
[weakSelf checkSelectedModels];
[weakSelf.collectionView reloadData]; [weakSelf.collectionView reloadData];
[weakSelf refreshBottomToolBarStatus]; [weakSelf refreshBottomToolBarStatus];
}; };
...@@ -440,9 +459,10 @@ static CGSize AssetGridThumbnailSize; ...@@ -440,9 +459,10 @@ static CGSize AssetGridThumbnailSize;
} }
- (void)scrollCollectionViewToBottom { - (void)scrollCollectionViewToBottom {
if (_shouldScrollToBottom && _models.count > 0) { TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
if (_shouldScrollToBottom && _models.count > 0 && tzImagePickerVc.sortAscendingByModificationDate) {
NSInteger item = _models.count - 1; NSInteger item = _models.count - 1;
if ([_model.name isEqualToString:@"相机胶卷"] || [_model.name isEqualToString:@"Camera Roll"] || [_model.name isEqualToString:@"所有照片"] ) { if (_showTakePhotoBtn) {
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController; TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
if (tzImagePickerVc.allowPickingImage && tzImagePickerVc.allowTakePicture) { if (tzImagePickerVc.allowPickingImage && tzImagePickerVc.allowTakePicture) {
item += 1; item += 1;
...@@ -489,11 +509,17 @@ static CGSize AssetGridThumbnailSize; ...@@ -489,11 +509,17 @@ static CGSize AssetGridThumbnailSize;
[[TZImageManager manager] getAssetsFromFetchResult:_model.result allowPickingVideo:tzImagePickerVc.allowPickingVideo allowPickingImage:tzImagePickerVc.allowPickingImage completion:^(NSArray<TZAssetModel *> *models) { [[TZImageManager manager] getAssetsFromFetchResult:_model.result allowPickingVideo:tzImagePickerVc.allowPickingVideo allowPickingImage:tzImagePickerVc.allowPickingImage completion:^(NSArray<TZAssetModel *> *models) {
[tzImagePickerVc hideProgressHUD]; [tzImagePickerVc hideProgressHUD];
TZAssetModel *model = [models lastObject]; TZAssetModel *assetModel;
[_models addObject:model]; if (tzImagePickerVc.sortAscendingByModificationDate) {
assetModel = [models lastObject];
[_models addObject:assetModel];
} else {
assetModel = [models firstObject];
[_models insertObject:assetModel atIndex:0];
}
if (tzImagePickerVc.selectedModels.count < tzImagePickerVc.maxImagesCount) { if (tzImagePickerVc.selectedModels.count < tzImagePickerVc.maxImagesCount) {
model.isSelected = YES; assetModel.isSelected = YES;
[tzImagePickerVc.selectedModels addObject:model]; [tzImagePickerVc.selectedModels addObject:assetModel];
[self refreshBottomToolBarStatus]; [self refreshBottomToolBarStatus];
} }
[_collectionView reloadData]; [_collectionView reloadData];
......
...@@ -122,6 +122,8 @@ ...@@ -122,6 +122,8 @@
- (IBAction)pickPhotoButtonClick:(UIButton *)sender { - (IBAction)pickPhotoButtonClick:(UIButton *)sender {
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9 delegate:self]; TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9 delegate:self];
imagePickerVc.isSelectOriginalPhoto = _isSelectOriginalPhoto; imagePickerVc.isSelectOriginalPhoto = _isSelectOriginalPhoto;
// tip:如果你需要将拍照放在外面,不要传这个参数
imagePickerVc.selectedAssets = _selectedAssets; // optional, 可选的 imagePickerVc.selectedAssets = _selectedAssets; // optional, 可选的
// imagePickerVc.allowTakePicture = NO; // 隐藏拍照按钮 // imagePickerVc.allowTakePicture = NO; // 隐藏拍照按钮
...@@ -143,6 +145,8 @@ ...@@ -143,6 +145,8 @@
// imagePickerVc.allowPickingImage = NO; // imagePickerVc.allowPickingImage = NO;
// imagePickerVc.allowPickingOriginalPhoto = NO; // imagePickerVc.allowPickingOriginalPhoto = NO;
imagePickerVc.sortAscendingByModificationDate = NO;
[self presentViewController:imagePickerVc animated:YES completion:nil]; [self presentViewController:imagePickerVc animated:YES completion:nil];
} }
......
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