cdts/xdts-ios 3/TreeHole/Code/Utility/ImagePicker/TZImagePickerController+MTImagePicker.m

324 lines
15 KiB
Mathematica
Raw Normal View History

2023-07-27 09:20:00 +08:00
//
// TZImagePickerController+MTImagePicker.m
// Meet
//
// Created by ko1o on 2018/9/14.
// Copyright © 2018 ko1o. All rights reserved.
//
#import "TZImagePickerController+MTImagePicker.h"
#import "UIImage+Extender.h"
#import "UIControl+BlocksKit.h"
#import "MTAlertView.h"
#import "UIImage+PHAsset.h"
#import "FLAnimatedImage.h"
#import "TZImagePickerController/TZAssetCell.h"
CGFloat kMTImagePickerDefaultWidthHeightRatio = (660 / 992.0 );
@interface TZImagePickerController (MTImagePicker)
- (void)configDefaultSetting;
@end
@implementation TZImagePickerController (MTImagePicker)
+ (TZImagePickerController *)mt_imagePickerWithMaxImagesCount:(NSInteger)maxImagesCount
didFinishPickingPhotosHandle:(void (^)(NSArray<UIImage *> *photos,NSArray *assets,BOOL isSelectOriginalPhoto))didFinishPickingPhotosHandle
{
TZImagePickerController *imagePicker = [self mt_imagePickerWithMaxImagesCount:maxImagesCount delegate:nil];
WeakSelf(imagePicker);
imagePicker.didFinishPickingPhotosHandle = ^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
if (didFinishPickingPhotosHandle) {
[weakimagePicker handleImagesWithAssets:assets resizedPhotos:photos completion:^(NSArray<id> *photos, NSArray *assets) {
didFinishPickingPhotosHandle(photos, assets, isSelectOriginalPhoto);
}];
}
};
imagePicker.didFinishPickingGifImageHandle = ^(UIImage *animatedImage, id sourceAssets) {
if (didFinishPickingPhotosHandle) {
didFinishPickingPhotosHandle(@[animatedImage], @[sourceAssets], NO);
}
};
return imagePicker;
}
+ (TZImagePickerController *)mt_imagePickerWithMaxImagesCount:(NSInteger)maxImagesCount
delegate:(id<TZImagePickerControllerDelegate>)delegate
pushPhotoPickerVc:(BOOL)pushPhotoPickerVc
{
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:maxImagesCount columnNumber:3 delegate:delegate pushPhotoPickerVc:pushPhotoPickerVc];
[imagePickerVc defaultSetup];
// [imagePickerVc setMt_naviBarHidden:imagePickerVc.maxImagesCount <= 1]; //
imagePickerVc.showSelectBtn = YES;
return imagePickerVc;
}
+ (TZImagePickerController *)mt_videoPickerWithDidFinishPickVideoHandle:(void (^)(UIImage *coverImage, PHAsset *asset))didFinishPickVideoHandle
{
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 columnNumber:3 delegate:nil pushPhotoPickerVc:YES];
[imagePickerVc defaultSetup];
imagePickerVc.allowPickingVideo = YES;
imagePickerVc.allowPickingImage = NO;
imagePickerVc.allowPickingGif = NO;
imagePickerVc.showSelectBtn = YES;
imagePickerVc.didFinishPickingVideoHandle = didFinishPickVideoHandle;
return imagePickerVc;
}
+ (TZImagePickerController *)mt_videoPreviewWithSelectedAsset:(PHAsset *)asset
didFinishPickVideoHandle:(void (^)(UIImage *coverImage, PHAsset *asset))didFinishPickVideoHandle {
if (!asset) {
return nil;
}
TZVideoPlayerController *videoPlayer = [[TZVideoPlayerController alloc] init];
videoPlayer.model = [[TZImageManager manager] createModelWithAsset:asset];
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithRootViewController:videoPlayer];
[imagePickerVc configDefaultSetting];
__weak typeof(imagePickerVc) _weakPicker = imagePickerVc;
imagePickerVc.didFinishPickingVideoHandle = didFinishPickVideoHandle;
return imagePickerVc;
}
+ (TZImagePickerController *)mt_imagePickerWithMaxImagesCount:(NSInteger)maxImagesCount delegate:(id<TZImagePickerControllerDelegate>)delegate
{
return [self mt_imagePickerWithMaxImagesCount:maxImagesCount delegate:delegate pushPhotoPickerVc:YES];
}
+ (TZImagePickerController *)mt_imagePreviewWithSelectedAssets:(NSMutableArray *)selectedAssets
selectedPhotos:(NSMutableArray *)selectedPhotos
maxImagesCount:(NSInteger)maxImagesCount
index:(NSInteger)index
didFinishPreviewPhotosHandle:(void(^)(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto))didFinishPreviewPhotosHandle
{
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithSelectedAssets:selectedAssets selectedPhotos:selectedPhotos index:index];
imagePickerVc.maxImagesCount = maxImagesCount;
[imagePickerVc defaultSetup];
imagePickerVc.showSelectBtn = YES;
imagePickerVc.statusBarStyle = UIStatusBarStyleLightContent;
WeakSelf(imagePickerVc);
imagePickerVc.didFinishPickingPhotosHandle = ^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
if (didFinishPreviewPhotosHandle) {
[weakimagePickerVc handleImagesWithAssets:assets resizedPhotos:photos completion:^(NSArray<id> *photos, NSArray *assets) {
didFinishPreviewPhotosHandle(photos, assets, isSelectOriginalPhoto);
}];
}
};
return imagePickerVc;
}
// UIImage or FLAniamteImage
- (void)handleImagesWithAssets:(NSArray *)assets resizedPhotos:(NSArray *)photos completion:(void(^)(NSArray<id> *photos, NSArray *assets))completion {
NSArray *datas = assets;
if (datas.count > 0 && assets.count == photos.count) {
NSMutableArray *assetIdArr = [NSMutableArray array];
NSMutableDictionary *imgDic = [NSMutableDictionary dictionary];
[datas enumerateObjectsUsingBlock:^(PHAsset * _Nonnull asset, NSUInteger idx, BOOL * _Nonnull stop) {
[assetIdArr addObject:asset.localIdentifier];
[UIImage getImageDataFromAsset:asset completion:^(NSData * _Nonnull data, NSString * _Nonnull suffix) {
id image = nil;
if ([[suffix lowercaseString] hasSuffix:@"gif"]) { // GIF
image = [[FLAnimatedImage alloc] initWithAnimatedGIFData:data optimalFrameCacheSize:3 predrawingEnabled:YES];
} else {
image = photos[idx];
}
if (image) {
[imgDic setValue:image forKey:asset.localIdentifier];
if ([imgDic allValues].count == datas.count) { //
NSMutableArray *imagesM = [NSMutableArray array];
[assetIdArr enumerateObjectsUsingBlock:^(NSString * _Nonnull assetId, NSUInteger idx, BOOL * _Nonnull stop) {
id image = imgDic[assetId];
if (image) {
[imagesM addObject:image];
}
}];
if (completion) {
completion(imagesM, assets);
}
}
} else {
*stop = YES;
NSLog(@"图片解析异常!!!!");
[SVProgressHUD showErrorWithStatus:@"图片解析异常~"];
if (completion) {
completion(photos, assets);
}
}
}];
}];
}
}
+ (BOOL)showAlbumAuthorizeIfNeed
{
if ([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusDenied) {
NSDictionary *infoDict = [TZCommonTools tz_getInfoDictionary];
NSString *appName = [infoDict valueForKey:@"CFBundleDisplayName"];
if (!appName) appName = [infoDict valueForKey:@"CFBundleName"];
NSString *tipText = [NSString stringWithFormat:[NSBundle tz_localizedStringForKey:@"Allow %@ to access your album in \"Settings -> Privacy -> Photos\""],appName];
[MTAlertView showWithSetupBlcok:^(MTAlertViewConfig *config) {
config.message = tipText;
config.cancelTitle = kMTAlertViewCancelTitle;
config.otherTitle = @"去授权";
config.otherHandler = ^(MTAlertButton *button) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:nil completionHandler:nil];
};
}];
return YES;
} else if ([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusNotDetermined) {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block bool isAllow = NO;
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
isAllow = YES;
} else{
isAllow = NO; //线
}
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
return !isAllow;
}
return NO;
}
- (void)defaultSetup
{
TZImagePickerController *imagePickerVc = self;
imagePickerVc.naviBgColor = [UIColor colorWithWhite:1 alpha:0.5];
imagePickerVc.naviTitleColor = [UIColor blackColor];
imagePickerVc.barItemTextColor = [UIColor blackColor];
imagePickerVc.naviTitleFont = MT_FONT_MEDIUM_SIZE(18);
imagePickerVc.barItemTextFont = MT_FONT_MEDIUM_SIZE(17);
imagePickerVc.statusBarStyle = UIStatusBarStyleDefault;
imagePickerVc.modalPresentationStyle = UIModalPresentationOverFullScreen;
imagePickerVc.navigationBar.shadowImage = [UIImage imageWithColor:COLOR_WITH_RGB(0xE9E9E9) size:CGSizeMake(SCREEN_WIDTH, 1)];
imagePickerVc.navigationBar.translucent = YES;
// imagePickerVc.navLeftBarButtonSettingBlock = ^(UIButton *leftButton) {
// leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
// [leftButton setImage:ImageNamed(@"publish_imagepicker_back_icon") forState:UIControlStateNormal];
// };
imagePickerVc.photoWidth = 1024;
// imagePickerVc.allowPreview = NO;
imagePickerVc.allowTakeVideo = NO;
imagePickerVc.allowPickingVideo = NO;
imagePickerVc.allowPickingOriginalPhoto = NO;//
imagePickerVc.allowPickingGif = NO;
imagePickerVc.allowPickingMultipleVideo = NO;
imagePickerVc.allowTakePicture = NO;
imagePickerVc.showSelectBtn = YES;
// 2. Set the appearance
// 2. imagePickerVc
imagePickerVc.oKButtonTitleColorDisabled = [UIColor colorWithWhite:1.0 alpha:0.5];
imagePickerVc.oKButtonTitleColorNormal = [UIColor whiteColor];
imagePickerVc.naviBgColor = BG_COLOR;
imagePickerVc.naviTitleColor = [UIColor whiteColor];
imagePickerVc.barItemTextColor = [UIColor whiteColor];
imagePickerVc.barItemTextFont = MT_FONT_REGULAR_SIZE(15);
imagePickerVc.iconThemeColor = [UIColor colorWithRed:31 / 255.0 green:185 / 255.0 blue:34 / 255.0 alpha:1.0];
imagePickerVc.showPhotoCannotSelectLayer = YES;
imagePickerVc.cannotSelectLayerColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8];
__weak TZImagePickerController *weakPicker = imagePickerVc;
imagePickerVc.photoPreviewPageUIConfigBlock = ^(UICollectionView *collectionView, UIView *naviBar, UIButton *backButton, UIButton *selectButton, UILabel *indexLabel, UIView *toolBar, UIButton *originalPhotoButton, UILabel *originalPhotoLabel, UIButton *doneButton, UIImageView *numberImageView, UILabel *numberLabel) {
if ([weakPicker mt_naviBarHidden]) {
naviBar.hidden = YES;
UIButton * _backButton = [[UIButton alloc] initWithFrame:CGRectZero];
[_backButton setTitleColor:weakPicker.oKButtonTitleColorNormal forState:UIControlStateNormal];
[_backButton bk_addEventHandler:^(id _Nonnull sender) {
[backButton sendActionsForControlEvents:UIControlEventTouchUpInside]; //
} forControlEvents:(UIControlEvents)UIControlEventTouchUpInside];
_backButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:18];
[_backButton setTitle:weakPicker.cancelBtnTitleStr forState:UIControlStateNormal];
[toolBar addSubview:_backButton];
_backButton.tag = 999;
}
};
imagePickerVc.albumPickerPageDidLayoutSubviewsBlock = ^(UITableView *tableView) {
tableView.backgroundColor = BG_COLOR;
};
imagePickerVc.albumCellDidLayoutSubviewsBlock = ^(TZAlbumCell *cell, UIImageView *posterImageView, UILabel *titleLabel) {
cell.backgroundColor = cell.contentView.backgroundColor = [UIColor clearColor];
};
imagePickerVc.photoPickerPageDidLayoutSubviewsBlock = ^(UICollectionView *collectionView, UIView *bottomToolBar, UIButton *previewButton, UIButton *originalPhotoButton, UILabel *originalPhotoLabel, UIButton *doneButton, UIImageView *numberImageView, UILabel *numberLabel, UIView *divideLine) {
collectionView.backgroundColor = BG_COLOR;
bottomToolBar.backgroundColor = BG_COLOR;
divideLine.backgroundColor = LINE_COLOR;
};
imagePickerVc.photoPreviewPageDidLayoutSubviewsBlock = ^(UICollectionView *collectionView, UIView *naviBar, UIButton *backButton, UIButton *selectButton, UILabel *indexLabel, UIView *toolBar, UIButton *originalPhotoButton, UILabel *originalPhotoLabel, UIButton *doneButton, UIImageView *numberImageView, UILabel *numberLabel) {
UIButton * _backButton = [toolBar viewWithTag:999];
if (_backButton) {
[_backButton sizeToFit];
_backButton.frame = CGRectMake(12, 0, _backButton.width, doneButton.height);
}
collectionView.backgroundColor = BG_COLOR;
toolBar.backgroundColor = BG_COLOR;
toolBar.height = MAX(44 + homeIndicatorHeight(), doneButton.height) ;
toolBar.y = self.view.height - toolBar.height;
if ([weakPicker mt_naviBarHidden]) {
naviBar.hidden = YES;
}
};
}
- (BOOL)mt_naviBarHidden
{
NSNumber *value = objc_getAssociatedObject(self, @selector(mt_naviBarHidden));
return [value boolValue];
}
- (void)setMt_naviBarHidden:(BOOL)mt_naviBarHidden
{
objc_setAssociatedObject(self, @selector(mt_naviBarHidden), @(mt_naviBarHidden), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
+ (void)mt_setImagePickerW3H4CropRect:(TZImagePickerController *)imagePickerVc showBottomHeartIcon:(BOOL)show
{
CGRect fullRect = [UIScreen mainScreen].bounds;
NSInteger left = FIX_SIZE(45 * 0.5);
NSInteger width = CGRectGetWidth(fullRect) - 2 * left;
NSInteger height = width / kMTImagePickerDefaultWidthHeightRatio;
NSInteger top = (CGRectGetHeight(fullRect) - height /* - HOME_INDICATOR_HEIHT - NAVIGATION_BAR_HEIGHT */) * 0.5;
imagePickerVc.allowCrop = YES;
imagePickerVc.cropRect = CGRectMake(left, top, width, height);
// imagePickerVc.mt_cropBorderRadius = FIX_SIZE(10);
imagePickerVc.cropViewSettingBlock = ^(UIView *cropView) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:ImageNamed(show ? @"facereport_add_bottom_icon": @"")];
[cropView addSubview:imageView];
cropView.layer.cornerRadius = FIX_SIZE(10);
cropView.layer.borderWidth = 1.5;
// cropView.backgroundColor = [UIColor redColor];
imageView.x = FIX_SIZE(15);
imageView.width = cropView.width - imageView.x * 2;
imageView.height = imageView.width * 190.0 / 659;
imageView.y = cropView.height - FIX_SIZE(15) - imageView.height;
};
}
@end