Commit f1e0071d authored by gewill's avatar gewill

添加 allowPickingImage 开关

parent 34aa3899
......@@ -24,12 +24,12 @@
- (BOOL)authorizationStatusAuthorized;
/// Get Album 获得相册/相册数组
- (void)getCameraRollAlbum:(BOOL)allowPickingVideo completion:(void (^)(TZAlbumModel *model))completion;
- (void)getAllAlbums:(BOOL)allowPickingVideo completion:(void (^)(NSArray<TZAlbumModel *> *models))completion;
- (void)getCameraRollAlbum:(BOOL)allowPickingVideo allowPickingImage:(BOOL)allowPickingImage completion:(void (^)(TZAlbumModel *model))completion;
- (void)getAllAlbums:(BOOL)allowPickingVideo allowPickingImage:(BOOL)allowPickingImage completion:(void (^)(NSArray<TZAlbumModel *> *models))completion;
/// Get Assets 获得Asset数组
- (void)getAssetsFromFetchResult:(id)result allowPickingVideo:(BOOL)allowPickingVideo completion:(void (^)(NSArray<TZAssetModel *> *models))completion;
- (void)getAssetFromFetchResult:(id)result atIndex:(NSInteger)index allowPickingVideo:(BOOL)allowPickingVideo completion:(void (^)(TZAssetModel *model))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 allowPickingImage:(BOOL)allowPickingImage completion:(void (^)(TZAssetModel *model))completion;
/// Get photo 获得照片
- (void)getPostImageWithAlbumModel:(TZAlbumModel *)model completion:(void (^)(UIImage *postImage))completion;
......
......@@ -45,13 +45,17 @@
#pragma mark - 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;
if (iOS8Later) {
PHFetchOptions *option = [[PHFetchOptions alloc] init];
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]];
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];
for (PHAssetCollection *collection in smartAlbums) {
if ([collection.localizedTitle isEqualToString:@"Camera Roll"] || [collection.localizedTitle isEqualToString:@"相机胶卷"]) {
......@@ -63,6 +67,7 @@
}
} else {
[self.assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if ([group numberOfAssets] < 1) return;
NSString *name = [group valueForProperty:ALAssetsGroupPropertyName];
if ([name isEqualToString:@"Camera Roll"] || [name isEqualToString:@"相机胶卷"]) {
......@@ -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];
if (iOS8Later) {
PHFetchOptions *option = [[PHFetchOptions alloc] init];
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]];
PHAssetCollectionSubtype smartAlbumSubtype = PHAssetCollectionSubtypeSmartAlbumUserLibrary | PHAssetCollectionSubtypeSmartAlbumRecentlyAdded | PHAssetCollectionSubtypeSmartAlbumVideos;
......@@ -130,7 +137,7 @@
#pragma mark - 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];
if ([result isKindOfClass:[PHFetchResult class]]) {
PHFetchResult *fetchResult = (PHFetchResult *)result;
......@@ -145,15 +152,18 @@
}
}
if (!allowPickingVideo && type == TZAssetModelMediaTypeVideo) return;
if (!allowPickingImage && type == TZAssetModelMediaTypePhoto) return;
NSString *timeLength = type == TZAssetModelMediaTypeVideo ? [NSString stringWithFormat:@"%0.0f",asset.duration] : @"";
timeLength = [self getNewTimeFromDurationSecond:timeLength.integerValue];
[photoArr addObject:[TZAssetModel modelWithAsset:asset type:type timeLength:timeLength]];
}];
if (completion) completion(photoArr);
} else if ([result isKindOfClass:[ALAssetsGroup class]]) {
ALAssetsGroup *gruop = (ALAssetsGroup *)result;
if (!allowPickingVideo) [gruop setAssetsFilter:[ALAssetsFilter allPhotos]];
[gruop enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
ALAssetsGroup *group = (ALAssetsGroup *)result;
if (!allowPickingVideo) [group setAssetsFilter:[ALAssetsFilter allPhotos]];
if (!allowPickingImage) [group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result == nil) {
if (completion) completion(photoArr);
}
......@@ -162,6 +172,11 @@
[photoArr addObject:[TZAssetModel modelWithAsset:result type:type]];
return;
}
if (!allowPickingImage){
[photoArr addObject:[TZAssetModel modelWithAsset:result type:type]];
return;
}
/// Allow picking video
if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
type = TZAssetModelMediaTypeVideo;
......@@ -177,7 +192,7 @@
}
/// 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]]) {
PHFetchResult *fetchResult = (PHFetchResult *)result;
PHAsset *asset = fetchResult[index];
......@@ -195,10 +210,11 @@
TZAssetModel *model = [TZAssetModel modelWithAsset:asset type:type timeLength:timeLength];
if (completion) completion(model);
} else if ([result isKindOfClass:[ALAssetsGroup class]]) {
ALAssetsGroup *gruop = (ALAssetsGroup *)result;
if (!allowPickingVideo) [gruop setAssetsFilter:[ALAssetsFilter allPhotos]];
ALAssetsGroup *group = (ALAssetsGroup *)result;
if (!allowPickingVideo) [group setAssetsFilter:[ALAssetsFilter allPhotos]];
if (!allowPickingImage) [group setAssetsFilter:[ALAssetsFilter allVideos]];
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;
TZAssetModelMediaType type = TZAssetModelMediaTypePhoto;
if (!allowPickingVideo){
......@@ -206,6 +222,12 @@
if (completion) completion(model);
return;
}
if (!allowPickingImage){
model = [TZAssetModel modelWithAsset:result type:type];
if (completion) completion(model);
return;
}
/// Allow picking video
if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
type = TZAssetModelMediaTypeVideo;
......@@ -339,8 +361,8 @@
if (completion) completion(photo);
}];
} else {
ALAssetsGroup *gruop = model.result;
UIImage *postImage = [UIImage imageWithCGImage:gruop.posterImage];
ALAssetsGroup *group = model.result;
UIImage *postImage = [UIImage imageWithCGImage:group.posterImage];
if (completion) completion(postImage);
}
}
......@@ -399,8 +421,8 @@
PHFetchResult *fetchResult = (PHFetchResult *)result;
model.count = fetchResult.count;
} else if ([result isKindOfClass:[ALAssetsGroup class]]) {
ALAssetsGroup *gruop = (ALAssetsGroup *)result;
model.count = [gruop numberOfAssets];
ALAssetsGroup *group = (ALAssetsGroup *)result;
model.count = [group numberOfAssets];
}
return model;
}
......
......@@ -30,6 +30,10 @@
/// 默认为YES,如果设置为NO,用户将不能选择发送视频
@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)showProgressHUD;
- (void)hideProgressHUD;
......
......@@ -81,6 +81,7 @@
// 默认准许用户选择原图和视频, 你也可以在这个方法后置为NO
_allowPickingOriginalPhoto = YES;
_allowPickingVideo = YES;
_allowPickingImage = YES;
if (![[TZImageManager manager] authorizationStatusAuthorized]) {
_tipLable = [[UILabel alloc] init];
......@@ -115,7 +116,7 @@
_pushToPhotoPickerVc = YES;
if (_pushToPhotoPickerVc) {
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;
[self pushViewController:photoPickerVc animated:YES];
_pushToPhotoPickerVc = NO;
......@@ -216,7 +217,7 @@
- (void)configTableView {
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];
CGFloat top = 44;
......
......@@ -49,7 +49,7 @@ static CGSize AssetGridThumbnailSize;
self.navigationItem.title = _model.name;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
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];
[self configCollectionView];
[self configBottomToolBar];
......
......@@ -88,7 +88,8 @@
// Set allow picking video & originalPhoto or not
// 设置是否可以选择视频/原图
// imagePickerVc.allowPickingVideo = NO;
// imagePickerVc.allowPickingVideo = NO;
// imagePickerVc.allowPickingImage = NO;
// imagePickerVc.allowPickingOriginalPhoto = NO;
[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