Commit 21454c38 authored by Zhen Tan's avatar Zhen Tan

Merge pull request #55 from gewill/master

添加 allowPickingImage 开关
parents 34aa3899 f1e0071d
...@@ -24,12 +24,12 @@ ...@@ -24,12 +24,12 @@
- (BOOL)authorizationStatusAuthorized; - (BOOL)authorizationStatusAuthorized;
/// Get Album 获得相册/相册数组 /// Get Album 获得相册/相册数组
- (void)getCameraRollAlbum:(BOOL)allowPickingVideo completion:(void (^)(TZAlbumModel *model))completion; - (void)getCameraRollAlbum:(BOOL)allowPickingVideo allowPickingImage:(BOOL)allowPickingImage completion:(void (^)(TZAlbumModel *model))completion;
- (void)getAllAlbums:(BOOL)allowPickingVideo completion:(void (^)(NSArray<TZAlbumModel *> *models))completion; - (void)getAllAlbums:(BOOL)allowPickingVideo allowPickingImage:(BOOL)allowPickingImage completion:(void (^)(NSArray<TZAlbumModel *> *models))completion;
/// Get Assets 获得Asset数组 /// Get Assets 获得Asset数组
- (void)getAssetsFromFetchResult:(id)result allowPickingVideo:(BOOL)allowPickingVideo completion:(void (^)(NSArray<TZAssetModel *> *models))completion; - (void)getAssetsFromFetchResult:(id)result allowPickingVideo:(BOOL)allowPickingVideo allowPickingImage:(BOOL)allowPickingImage completion:(void (^)(NSArray<TZAssetModel *> *models))completion;
- (void)getAssetFromFetchResult:(id)result atIndex:(NSInteger)index allowPickingVideo:(BOOL)allowPickingVideo completion:(void (^)(TZAssetModel *model))completion; - (void)getAssetFromFetchResult:(id)result atIndex:(NSInteger)index allowPickingVideo:(BOOL)allowPickingVideo allowPickingImage:(BOOL)allowPickingImage completion:(void (^)(TZAssetModel *model))completion;
/// Get photo 获得照片 /// Get photo 获得照片
- (void)getPostImageWithAlbumModel:(TZAlbumModel *)model completion:(void (^)(UIImage *postImage))completion; - (void)getPostImageWithAlbumModel:(TZAlbumModel *)model completion:(void (^)(UIImage *postImage))completion;
......
...@@ -45,13 +45,17 @@ ...@@ -45,13 +45,17 @@
#pragma mark - Get Album #pragma mark - Get Album
/// Get Album 获得相册/相册数组 /// Get Album 获得相册/相册数组
- (void)getCameraRollAlbum:(BOOL)allowPickingVideo completion:(void (^)(TZAlbumModel *))completion{ - (void)getCameraRollAlbum:(BOOL)allowPickingVideo allowPickingImage:(BOOL)allowPickingImage completion:(void (^)(TZAlbumModel *))completion{
__block TZAlbumModel *model; __block TZAlbumModel *model;
if (iOS8Later) { if (iOS8Later) {
PHFetchOptions *option = [[PHFetchOptions alloc] init]; PHFetchOptions *option = [[PHFetchOptions alloc] init];
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",
PHAssetMediaTypeVideo];
option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]]; option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
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:@"相机胶卷"]) { if ([collection.localizedTitle isEqualToString:@"Camera Roll"] || [collection.localizedTitle isEqualToString:@"相机胶卷"]) {
...@@ -63,6 +67,7 @@ ...@@ -63,6 +67,7 @@
} }
} else { } else {
[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:@"相机胶卷"]) { if ([name isEqualToString:@"Camera Roll"] || [name isEqualToString:@"相机胶卷"]) {
...@@ -74,11 +79,13 @@ ...@@ -74,11 +79,13 @@
} }
} }
- (void)getAllAlbums:(BOOL)allowPickingVideo completion:(void (^)(NSArray<TZAlbumModel *> *))completion{ - (void)getAllAlbums:(BOOL)allowPickingVideo allowPickingImage:(BOOL)allowPickingImage completion:(void (^)(NSArray<TZAlbumModel *> *))completion{
NSMutableArray *albumArr = [NSMutableArray array]; NSMutableArray *albumArr = [NSMutableArray array];
if (iOS8Later) { if (iOS8Later) {
PHFetchOptions *option = [[PHFetchOptions alloc] init]; PHFetchOptions *option = [[PHFetchOptions alloc] init];
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",
PHAssetMediaTypeVideo];
option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]]; option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHAssetCollectionSubtype smartAlbumSubtype = PHAssetCollectionSubtypeSmartAlbumUserLibrary | PHAssetCollectionSubtypeSmartAlbumRecentlyAdded | PHAssetCollectionSubtypeSmartAlbumVideos; PHAssetCollectionSubtype smartAlbumSubtype = PHAssetCollectionSubtypeSmartAlbumUserLibrary | PHAssetCollectionSubtypeSmartAlbumRecentlyAdded | PHAssetCollectionSubtypeSmartAlbumVideos;
...@@ -130,7 +137,7 @@ ...@@ -130,7 +137,7 @@
#pragma mark - Get Assets #pragma mark - Get Assets
/// Get Assets 获得照片数组 /// Get Assets 获得照片数组
- (void)getAssetsFromFetchResult:(id)result allowPickingVideo:(BOOL)allowPickingVideo completion:(void (^)(NSArray<TZAssetModel *> *))completion { - (void)getAssetsFromFetchResult:(id)result allowPickingVideo:(BOOL)allowPickingVideo allowPickingImage:(BOOL)allowPickingImage completion:(void (^)(NSArray<TZAssetModel *> *))completion {
NSMutableArray *photoArr = [NSMutableArray array]; NSMutableArray *photoArr = [NSMutableArray array];
if ([result isKindOfClass:[PHFetchResult class]]) { if ([result isKindOfClass:[PHFetchResult class]]) {
PHFetchResult *fetchResult = (PHFetchResult *)result; PHFetchResult *fetchResult = (PHFetchResult *)result;
...@@ -145,15 +152,18 @@ ...@@ -145,15 +152,18 @@
} }
} }
if (!allowPickingVideo && type == TZAssetModelMediaTypeVideo) return; if (!allowPickingVideo && type == TZAssetModelMediaTypeVideo) return;
if (!allowPickingImage && type == TZAssetModelMediaTypePhoto) return;
NSString *timeLength = type == TZAssetModelMediaTypeVideo ? [NSString stringWithFormat:@"%0.0f",asset.duration] : @""; NSString *timeLength = type == TZAssetModelMediaTypeVideo ? [NSString stringWithFormat:@"%0.0f",asset.duration] : @"";
timeLength = [self getNewTimeFromDurationSecond:timeLength.integerValue]; timeLength = [self getNewTimeFromDurationSecond:timeLength.integerValue];
[photoArr addObject:[TZAssetModel modelWithAsset:asset type:type timeLength:timeLength]]; [photoArr addObject:[TZAssetModel modelWithAsset:asset type:type timeLength:timeLength]];
}]; }];
if (completion) completion(photoArr); if (completion) completion(photoArr);
} else if ([result isKindOfClass:[ALAssetsGroup class]]) { } else if ([result isKindOfClass:[ALAssetsGroup class]]) {
ALAssetsGroup *gruop = (ALAssetsGroup *)result; ALAssetsGroup *group = (ALAssetsGroup *)result;
if (!allowPickingVideo) [gruop setAssetsFilter:[ALAssetsFilter allPhotos]]; if (!allowPickingVideo) [group setAssetsFilter:[ALAssetsFilter allPhotos]];
[gruop enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { if (!allowPickingImage) [group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result == nil) { if (result == nil) {
if (completion) completion(photoArr); if (completion) completion(photoArr);
} }
...@@ -162,6 +172,11 @@ ...@@ -162,6 +172,11 @@
[photoArr addObject:[TZAssetModel modelWithAsset:result type:type]]; [photoArr addObject:[TZAssetModel modelWithAsset:result type:type]];
return; return;
} }
if (!allowPickingImage){
[photoArr addObject:[TZAssetModel modelWithAsset:result type:type]];
return;
}
/// Allow picking video /// Allow picking video
if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) { if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
type = TZAssetModelMediaTypeVideo; type = TZAssetModelMediaTypeVideo;
...@@ -177,7 +192,7 @@ ...@@ -177,7 +192,7 @@
} }
/// Get asset at index 获得下标为index的单个照片 /// Get asset at index 获得下标为index的单个照片
- (void)getAssetFromFetchResult:(id)result atIndex:(NSInteger)index allowPickingVideo:(BOOL)allowPickingVideo completion:(void (^)(TZAssetModel *))completion { - (void)getAssetFromFetchResult:(id)result atIndex:(NSInteger)index allowPickingVideo:(BOOL)allowPickingVideo allowPickingImage:(BOOL)allowPickingImage completion:(void (^)(TZAssetModel *))completion {
if ([result isKindOfClass:[PHFetchResult class]]) { if ([result isKindOfClass:[PHFetchResult class]]) {
PHFetchResult *fetchResult = (PHFetchResult *)result; PHFetchResult *fetchResult = (PHFetchResult *)result;
PHAsset *asset = fetchResult[index]; PHAsset *asset = fetchResult[index];
...@@ -195,10 +210,11 @@ ...@@ -195,10 +210,11 @@
TZAssetModel *model = [TZAssetModel modelWithAsset:asset type:type timeLength:timeLength]; TZAssetModel *model = [TZAssetModel modelWithAsset:asset type:type timeLength:timeLength];
if (completion) completion(model); if (completion) completion(model);
} else if ([result isKindOfClass:[ALAssetsGroup class]]) { } else if ([result isKindOfClass:[ALAssetsGroup class]]) {
ALAssetsGroup *gruop = (ALAssetsGroup *)result; ALAssetsGroup *group = (ALAssetsGroup *)result;
if (!allowPickingVideo) [gruop setAssetsFilter:[ALAssetsFilter allPhotos]]; if (!allowPickingVideo) [group setAssetsFilter:[ALAssetsFilter allPhotos]];
if (!allowPickingImage) [group setAssetsFilter:[ALAssetsFilter allVideos]];
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index]; NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index];
[gruop enumerateAssetsAtIndexes:indexSet options:NSEnumerationConcurrent usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { [group enumerateAssetsAtIndexes:indexSet options:NSEnumerationConcurrent usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
TZAssetModel *model; TZAssetModel *model;
TZAssetModelMediaType type = TZAssetModelMediaTypePhoto; TZAssetModelMediaType type = TZAssetModelMediaTypePhoto;
if (!allowPickingVideo){ if (!allowPickingVideo){
...@@ -206,6 +222,12 @@ ...@@ -206,6 +222,12 @@
if (completion) completion(model); if (completion) completion(model);
return; return;
} }
if (!allowPickingImage){
model = [TZAssetModel modelWithAsset:result type:type];
if (completion) completion(model);
return;
}
/// Allow picking video /// Allow picking video
if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) { if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
type = TZAssetModelMediaTypeVideo; type = TZAssetModelMediaTypeVideo;
...@@ -339,8 +361,8 @@ ...@@ -339,8 +361,8 @@
if (completion) completion(photo); if (completion) completion(photo);
}]; }];
} else { } else {
ALAssetsGroup *gruop = model.result; ALAssetsGroup *group = model.result;
UIImage *postImage = [UIImage imageWithCGImage:gruop.posterImage]; UIImage *postImage = [UIImage imageWithCGImage:group.posterImage];
if (completion) completion(postImage); if (completion) completion(postImage);
} }
} }
...@@ -399,8 +421,8 @@ ...@@ -399,8 +421,8 @@
PHFetchResult *fetchResult = (PHFetchResult *)result; PHFetchResult *fetchResult = (PHFetchResult *)result;
model.count = fetchResult.count; model.count = fetchResult.count;
} else if ([result isKindOfClass:[ALAssetsGroup class]]) { } else if ([result isKindOfClass:[ALAssetsGroup class]]) {
ALAssetsGroup *gruop = (ALAssetsGroup *)result; ALAssetsGroup *group = (ALAssetsGroup *)result;
model.count = [gruop numberOfAssets]; model.count = [group numberOfAssets];
} }
return model; return model;
} }
......
...@@ -30,6 +30,10 @@ ...@@ -30,6 +30,10 @@
/// 默认为YES,如果设置为NO,用户将不能选择发送视频 /// 默认为YES,如果设置为NO,用户将不能选择发送视频
@property (nonatomic, assign) BOOL allowPickingVideo; @property (nonatomic, assign) BOOL allowPickingVideo;
/// Default is YES.if set NO, user can't picking image.
/// 默认为YES,如果设置为NO,用户将不能选择发送图片
@property(nonatomic, assign) BOOL allowPickingImage;
- (void)showAlertWithTitle:(NSString *)title; - (void)showAlertWithTitle:(NSString *)title;
- (void)showProgressHUD; - (void)showProgressHUD;
- (void)hideProgressHUD; - (void)hideProgressHUD;
......
...@@ -81,6 +81,7 @@ ...@@ -81,6 +81,7 @@
// 默认准许用户选择原图和视频, 你也可以在这个方法后置为NO // 默认准许用户选择原图和视频, 你也可以在这个方法后置为NO
_allowPickingOriginalPhoto = YES; _allowPickingOriginalPhoto = YES;
_allowPickingVideo = YES; _allowPickingVideo = YES;
_allowPickingImage = YES;
if (![[TZImageManager manager] authorizationStatusAuthorized]) { if (![[TZImageManager manager] authorizationStatusAuthorized]) {
_tipLable = [[UILabel alloc] init]; _tipLable = [[UILabel alloc] init];
...@@ -115,7 +116,7 @@ ...@@ -115,7 +116,7 @@
_pushToPhotoPickerVc = YES; _pushToPhotoPickerVc = YES;
if (_pushToPhotoPickerVc) { if (_pushToPhotoPickerVc) {
TZPhotoPickerController *photoPickerVc = [[TZPhotoPickerController alloc] init]; TZPhotoPickerController *photoPickerVc = [[TZPhotoPickerController alloc] init];
[[TZImageManager manager] getCameraRollAlbum:self.allowPickingVideo 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];
_pushToPhotoPickerVc = NO; _pushToPhotoPickerVc = NO;
...@@ -216,7 +217,7 @@ ...@@ -216,7 +217,7 @@
- (void)configTableView { - (void)configTableView {
TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController; TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController;
[[TZImageManager manager] getAllAlbums:imagePickerVc.allowPickingVideo completion:^(NSArray<TZAlbumModel *> *models) { [[TZImageManager manager] getAllAlbums:imagePickerVc.allowPickingVideo allowPickingImage:imagePickerVc.allowPickingImage completion:^(NSArray<TZAlbumModel *> *models) {
_albumArr = [NSMutableArray arrayWithArray:models]; _albumArr = [NSMutableArray arrayWithArray:models];
CGFloat top = 44; CGFloat top = 44;
......
...@@ -49,7 +49,7 @@ static CGSize AssetGridThumbnailSize; ...@@ -49,7 +49,7 @@ static CGSize AssetGridThumbnailSize;
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)];
TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController; TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController;
[[TZImageManager manager] getAssetsFromFetchResult:_model.result allowPickingVideo:imagePickerVc.allowPickingVideo completion:^(NSArray<TZAssetModel *> *models) { [[TZImageManager manager] getAssetsFromFetchResult:_model.result allowPickingVideo:imagePickerVc.allowPickingVideo allowPickingImage:imagePickerVc.allowPickingImage completion:^(NSArray<TZAssetModel *> *models) {
_photoArr = [NSMutableArray arrayWithArray:models]; _photoArr = [NSMutableArray arrayWithArray:models];
[self configCollectionView]; [self configCollectionView];
[self configBottomToolBar]; [self configBottomToolBar];
......
...@@ -88,7 +88,8 @@ ...@@ -88,7 +88,8 @@
// Set allow picking video & originalPhoto or not // Set allow picking video & originalPhoto or not
// 设置是否可以选择视频/原图 // 设置是否可以选择视频/原图
// imagePickerVc.allowPickingVideo = NO; // imagePickerVc.allowPickingVideo = NO;
// imagePickerVc.allowPickingImage = NO;
// imagePickerVc.allowPickingOriginalPhoto = NO; // imagePickerVc.allowPickingOriginalPhoto = 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