| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608 |
- //
- // TZImagePickerController.m
- // TZImagePickerController
- //
- // Created by 谭真 on 15/12/24.
- // Copyright © 2015年 谭真. All rights reserved.
- // version 1.7.8 - 2016.12.20
- #import "TZImagePickerController.h"
- #import "TZPhotoPickerController.h"
- #import "TZPhotoPreviewController.h"
- #import "TZAssetModel.h"
- #import "TZAssetCell.h"
- #import "UIView+Layout.h"
- #import "TZImageManager.h"
- @interface TZImagePickerController () {
- NSTimer *_timer;
- UILabel *_tipLabel;
- UIButton *_settingBtn;
- BOOL _pushPhotoPickerVc;
- BOOL _didPushPhotoPickerVc;
-
- UIButton *_progressHUD;
- UIView *_HUDContainer;
- UIActivityIndicatorView *_HUDIndicatorView;
- UILabel *_HUDLabel;
-
- UIStatusBarStyle _originStatusBarStyle;
- }
- /// Default is 4, Use in photos collectionView in TZPhotoPickerController
- /// 默认4列, TZPhotoPickerController中的照片collectionView
- @property (nonatomic, assign) NSInteger columnNumber;
- @end
- @implementation TZImagePickerController
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wdeprecated-declarations"
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor whiteColor];
- self.navigationBar.barStyle = UIBarStyleBlack;
- self.navigationBar.translucent = YES;
- [TZImageManager manager].shouldFixOrientation = NO;
- // Default appearance, you can reset these after this method
- // 默认的外观,你可以在这个方法后重置
- self.oKButtonTitleColorNormal = [UIColor colorWithRed:(83/255.0) green:(179/255.0) blue:(17/255.0) alpha:1.0];
- self.oKButtonTitleColorDisabled = [UIColor colorWithRed:(83/255.0) green:(179/255.0) blue:(17/255.0) alpha:0.5];
-
- if (iOS7Later) {
- self.navigationBar.barTintColor = [UIColor colorWithRed:(34/255.0) green:(34/255.0) blue:(34/255.0) alpha:1.0];
- self.navigationBar.tintColor = [UIColor whiteColor];
- self.automaticallyAdjustsScrollViewInsets = NO;
- }
- }
- - (void)setNaviBgColor:(UIColor *)naviBgColor {
- _naviBgColor = naviBgColor;
- self.navigationBar.barTintColor = naviBgColor;
- }
- - (void)setNaviTitleColor:(UIColor *)naviTitleColor {
- _naviTitleColor = naviTitleColor;
- [self configNaviTitleAppearance];
- }
- - (void)setNaviTitleFont:(UIFont *)naviTitleFont {
- _naviTitleFont = naviTitleFont;
- [self configNaviTitleAppearance];
- }
- - (void)configNaviTitleAppearance {
- NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
- textAttrs[NSForegroundColorAttributeName] = self.naviTitleColor;
- textAttrs[NSFontAttributeName] = self.naviTitleFont;
- self.navigationBar.titleTextAttributes = textAttrs;
- }
- - (void)setBarItemTextFont:(UIFont *)barItemTextFont {
- _barItemTextFont = barItemTextFont;
- [self configBarButtonItemAppearance];
- }
- - (void)setBarItemTextColor:(UIColor *)barItemTextColor {
- _barItemTextColor = barItemTextColor;
- [self configBarButtonItemAppearance];
- }
- - (void)configBarButtonItemAppearance {
- UIBarButtonItem *barItem;
- if (iOS9Later) {
- barItem = [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[TZImagePickerController class]]];
- } else {
- barItem = [UIBarButtonItem appearanceWhenContainedIn:[TZImagePickerController class], nil];
- }
- NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
- textAttrs[NSForegroundColorAttributeName] = self.barItemTextColor;
- textAttrs[NSFontAttributeName] = self.barItemTextFont;
- [barItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- _originStatusBarStyle = [UIApplication sharedApplication].statusBarStyle;
- [UIApplication sharedApplication].statusBarStyle = iOS7Later ? UIStatusBarStyleLightContent : UIStatusBarStyleBlackOpaque;
- }
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
- [UIApplication sharedApplication].statusBarStyle = _originStatusBarStyle;
- [self hideProgressHUD];
- }
- - (instancetype)initWithMaxImagesCount:(NSInteger)maxImagesCount delegate:(id<TZImagePickerControllerDelegate>)delegate {
- return [self initWithMaxImagesCount:maxImagesCount columnNumber:4 delegate:delegate pushPhotoPickerVc:YES];
- }
- - (instancetype)initWithMaxImagesCount:(NSInteger)maxImagesCount columnNumber:(NSInteger)columnNumber delegate:(id<TZImagePickerControllerDelegate>)delegate {
- return [self initWithMaxImagesCount:maxImagesCount columnNumber:columnNumber delegate:delegate pushPhotoPickerVc:YES];
- }
- - (instancetype)initWithMaxImagesCount:(NSInteger)maxImagesCount columnNumber:(NSInteger)columnNumber delegate:(id<TZImagePickerControllerDelegate>)delegate pushPhotoPickerVc:(BOOL)pushPhotoPickerVc {
- _pushPhotoPickerVc = pushPhotoPickerVc;
- TZAlbumPickerController *albumPickerVc = [[TZAlbumPickerController alloc] init];
- albumPickerVc.columnNumber = columnNumber;
- self = [super initWithRootViewController:albumPickerVc];
- if (self) {
- self.maxImagesCount = maxImagesCount > 0 ? maxImagesCount : 9; // Default is 9 / 默认最大可选9张图片
- self.pickerDelegate = delegate;
- self.selectedModels = [NSMutableArray array];
-
- // Allow user picking original photo and video, you also can set No after this method
- // 默认准许用户选择原图和视频, 你也可以在这个方法后置为NO
- self.allowPickingOriginalPhoto = YES;
- self.allowPickingVideo = YES;
- self.allowPickingImage = YES;
- self.allowTakePicture = YES;
- self.sortAscendingByModificationDate = YES;
- self.autoDismiss = YES;
- self.columnNumber = columnNumber;
- [self configDefaultSetting];
-
- if (![[TZImageManager manager] authorizationStatusAuthorized]) {
- _tipLabel = [[UILabel alloc] init];
- _tipLabel.frame = CGRectMake(8, 120, self.view.tz_width - 16, 60);
- _tipLabel.textAlignment = NSTextAlignmentCenter;
- _tipLabel.numberOfLines = 0;
- _tipLabel.font = [UIFont systemFontOfSize:16];
- _tipLabel.textColor = [UIColor blackColor];
- NSString *appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleDisplayName"];
- if (!appName) appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleName"];
- NSString *tipText = [NSString stringWithFormat:[NSBundle tz_localizedStringForKey:@"Allow %@ to access your album in \"Settings -> Privacy -> Photos\""],appName];
- _tipLabel.text = tipText;
- [self.view addSubview:_tipLabel];
-
- _settingBtn = [UIButton buttonWithType:UIButtonTypeSystem];
- [_settingBtn setTitle:self.settingBtnTitleStr forState:UIControlStateNormal];
- _settingBtn.frame = CGRectMake(0, 180, self.view.tz_width, 44);
- _settingBtn.titleLabel.font = [UIFont systemFontOfSize:18];
- [_settingBtn addTarget:self action:@selector(settingBtnClick) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:_settingBtn];
-
- _timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(observeAuthrizationStatusChange) userInfo:nil repeats:YES];
- } else {
- [self pushPhotoPickerVc];
- }
- }
- return self;
- }
- /// This init method just for previewing photos / 用这个初始化方法以预览图片
- - (instancetype)initWithSelectedAssets:(NSMutableArray *)selectedAssets selectedPhotos:(NSMutableArray *)selectedPhotos index:(NSInteger)index{
- TZPhotoPreviewController *previewVc = [[TZPhotoPreviewController alloc] init];
- self = [super initWithRootViewController:previewVc];
- if (self) {
- self.selectedAssets = [NSMutableArray arrayWithArray:selectedAssets];
- self.allowPickingOriginalPhoto = self.allowPickingOriginalPhoto;
- [self configDefaultSetting];
- previewVc.photos = [NSMutableArray arrayWithArray:selectedPhotos];
- previewVc.currentIndex = index;
- __weak typeof(self) weakSelf = self;
- [previewVc setDoneButtonClickBlockWithPreviewType:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
- [weakSelf dismissViewControllerAnimated:YES completion:^{
- if (weakSelf.didFinishPickingPhotosHandle) {
- weakSelf.didFinishPickingPhotosHandle(photos,assets,isSelectOriginalPhoto);
- }
- }];
- }];
- }
- return self;
- }
- /// This init method for crop photo / 用这个初始化方法以裁剪图片
- - (instancetype)initCropTypeWithAsset:(id)asset photo:(UIImage *)photo completion:(void (^)(UIImage *cropImage,id asset))completion {
- TZPhotoPreviewController *previewVc = [[TZPhotoPreviewController alloc] init];
- self = [super initWithRootViewController:previewVc];
- if (self) {
- self.maxImagesCount = 1;
- self.allowCrop = YES;
- self.selectedAssets = [NSMutableArray arrayWithArray:@[asset]];
- [self configDefaultSetting];
-
- previewVc.photos = [NSMutableArray arrayWithArray:@[photo]];
- previewVc.isCropImage = YES;
- previewVc.currentIndex = 0;
- __weak typeof(self) weakSelf = self;
- [previewVc setDoneButtonClickBlockCropMode:^(UIImage *cropImage, id asset) {
- [weakSelf dismissViewControllerAnimated:YES completion:^{
- if (completion) {
- completion(cropImage,asset);
- }
- }];
- }];
- }
- return self;
- }
- - (void)configDefaultSetting {
- self.timeout = 15;
- self.photoWidth = 828.0;
- self.photoPreviewMaxWidth = 600;
- self.naviTitleColor = [UIColor whiteColor];
- self.naviTitleFont = [UIFont systemFontOfSize:17];
- self.barItemTextFont = [UIFont systemFontOfSize:15];
- self.barItemTextColor = [UIColor whiteColor];
- self.allowPreview = YES;
-
- [self configDefaultImageName];
- [self configDefaultBtnTitle];
- }
- - (void)configDefaultImageName {
- self.takePictureImageName = @"takePicture.png";
- self.photoSelImageName = @"photo_sel_photoPickerVc.png";
- self.photoDefImageName = @"photo_def_photoPickerVc.png";
- self.photoNumberIconImageName = @"photo_number_icon.png";
- self.photoPreviewOriginDefImageName = @"preview_original_def.png";
- self.photoOriginDefImageName = @"photo_original_def.png";
- self.photoOriginSelImageName = @"photo_original_sel.png";
- }
- - (void)configDefaultBtnTitle {
- self.doneBtnTitleStr = [NSBundle tz_localizedStringForKey:@"Done"];
- self.cancelBtnTitleStr = [NSBundle tz_localizedStringForKey:@"Cancel"];
- self.previewBtnTitleStr = [NSBundle tz_localizedStringForKey:@"Preview"];
- self.fullImageBtnTitleStr = [NSBundle tz_localizedStringForKey:@"Full image"];
- self.settingBtnTitleStr = [NSBundle tz_localizedStringForKey:@"Setting"];
- self.processHintStr = [NSBundle tz_localizedStringForKey:@"Processing..."];
- }
- - (void)observeAuthrizationStatusChange {
- if ([[TZImageManager manager] authorizationStatusAuthorized]) {
- [_tipLabel removeFromSuperview];
- [_settingBtn removeFromSuperview];
- [_timer invalidate];
- _timer = nil;
- [self pushPhotoPickerVc];
- }
- }
- - (void)pushPhotoPickerVc {
- _didPushPhotoPickerVc = NO;
- // 1.6.8 判断是否需要push到照片选择页,如果_pushPhotoPickerVc为NO,则不push
- if (!_didPushPhotoPickerVc && _pushPhotoPickerVc) {
- TZPhotoPickerController *photoPickerVc = [[TZPhotoPickerController alloc] init];
- photoPickerVc.isFirstAppear = YES;
- photoPickerVc.columnNumber = self.columnNumber;
- [[TZImageManager manager] getCameraRollAlbum:self.allowPickingVideo allowPickingImage:self.allowPickingImage completion:^(TZAlbumModel *model) {
- photoPickerVc.model = model;
- [self pushViewController:photoPickerVc animated:YES];
- _didPushPhotoPickerVc = YES;
- }];
- }
- }
- - (void)showAlertWithTitle:(NSString *)title {
- if (iOS8Later) {
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
- [alertController addAction:[UIAlertAction actionWithTitle:[NSBundle tz_localizedStringForKey:@"OK"] style:UIAlertActionStyleDefault handler:nil]];
- [self presentViewController:alertController animated:YES completion:nil];
- } else {
- [[[UIAlertView alloc] initWithTitle:title message:nil delegate:nil cancelButtonTitle:[NSBundle tz_localizedStringForKey:@"OK"] otherButtonTitles:nil, nil] show];
- }
- }
- - (void)showProgressHUD {
- if (!_progressHUD) {
- _progressHUD = [UIButton buttonWithType:UIButtonTypeCustom];
- [_progressHUD setBackgroundColor:[UIColor clearColor]];
- _HUDContainer = [[UIView alloc] init];
- _HUDContainer.frame = CGRectMake((self.view.tz_width - 120) / 2, (self.view.tz_height - 90) / 2, 120, 90);
- _HUDContainer.layer.cornerRadius = 8;
- _HUDContainer.clipsToBounds = YES;
- _HUDContainer.backgroundColor = [UIColor darkGrayColor];
- _HUDContainer.alpha = 0.7;
-
- _HUDIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
- _HUDIndicatorView.frame = CGRectMake(45, 15, 30, 30);
-
- _HUDLabel = [[UILabel alloc] init];
- _HUDLabel.frame = CGRectMake(0,40, 120, 50);
- _HUDLabel.textAlignment = NSTextAlignmentCenter;
- _HUDLabel.text = self.processHintStr;
- _HUDLabel.font = [UIFont systemFontOfSize:15];
- _HUDLabel.textColor = [UIColor whiteColor];
-
- [_HUDContainer addSubview:_HUDLabel];
- [_HUDContainer addSubview:_HUDIndicatorView];
- [_progressHUD addSubview:_HUDContainer];
- }
- [_HUDIndicatorView startAnimating];
- [[UIApplication sharedApplication].keyWindow addSubview:_progressHUD];
-
- // if over time, dismiss HUD automatic
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.timeout * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [self hideProgressHUD];
- });
- }
- - (void)hideProgressHUD {
- if (_progressHUD) {
- [_HUDIndicatorView stopAnimating];
- [_progressHUD removeFromSuperview];
- }
- }
- - (void)setMaxImagesCount:(NSInteger)maxImagesCount {
- _maxImagesCount = maxImagesCount;
- if (maxImagesCount > 1) {
- _showSelectBtn = YES;
- _allowCrop = NO;
- }
- }
- - (void)setShowSelectBtn:(BOOL)showSelectBtn {
- _showSelectBtn = showSelectBtn;
- // 多选模式下,不允许让showSelectBtn为NO
- if (!showSelectBtn && _maxImagesCount > 1) {
- _showSelectBtn = YES;
- }
- }
- - (void)setAllowCrop:(BOOL)allowCrop {
- _allowCrop = _maxImagesCount > 1 ? NO : allowCrop;
- if (allowCrop) { // 允许裁剪的时候,不能选原图和GIF
- self.allowPickingOriginalPhoto = NO;
- self.allowPickingGif = NO;
- }
- }
- - (void)setCircleCropRadius:(NSInteger)circleCropRadius {
- _circleCropRadius = circleCropRadius;
- _cropRect = CGRectMake(self.view.tz_width / 2 - circleCropRadius, self.view.tz_height / 2 - _circleCropRadius, _circleCropRadius * 2, _circleCropRadius * 2);
- }
- - (CGRect)cropRect {
- if (_cropRect.size.width > 0) {
- return _cropRect;
- }
- CGFloat cropViewWH = self.view.tz_width;
- return CGRectMake(0, (self.view.tz_height - self.view.tz_width) / 2, cropViewWH, cropViewWH);
- }
- - (void)setTimeout:(NSInteger)timeout {
- _timeout = timeout;
- if (timeout < 5) {
- _timeout = 5;
- } else if (_timeout > 60) {
- _timeout = 60;
- }
- }
- - (void)setColumnNumber:(NSInteger)columnNumber {
- _columnNumber = columnNumber;
- if (columnNumber <= 2) {
- _columnNumber = 2;
- } else if (columnNumber >= 6) {
- _columnNumber = 6;
- }
-
- TZAlbumPickerController *albumPickerVc = [self.childViewControllers firstObject];
- albumPickerVc.columnNumber = _columnNumber;
- [TZImageManager manager].columnNumber = _columnNumber;
- }
- - (void)setMinPhotoWidthSelectable:(NSInteger)minPhotoWidthSelectable {
- _minPhotoWidthSelectable = minPhotoWidthSelectable;
- [TZImageManager manager].minPhotoWidthSelectable = minPhotoWidthSelectable;
- }
- - (void)setMinPhotoHeightSelectable:(NSInteger)minPhotoHeightSelectable {
- _minPhotoHeightSelectable = minPhotoHeightSelectable;
- [TZImageManager manager].minPhotoHeightSelectable = minPhotoHeightSelectable;
- }
- - (void)setHideWhenCanNotSelect:(BOOL)hideWhenCanNotSelect {
- _hideWhenCanNotSelect = hideWhenCanNotSelect;
- [TZImageManager manager].hideWhenCanNotSelect = hideWhenCanNotSelect;
- }
- - (void)setPhotoPreviewMaxWidth:(CGFloat)photoPreviewMaxWidth {
- _photoPreviewMaxWidth = photoPreviewMaxWidth;
- if (photoPreviewMaxWidth > 800) {
- _photoPreviewMaxWidth = 800;
- } else if (photoPreviewMaxWidth < 500) {
- _photoPreviewMaxWidth = 500;
- }
- [TZImageManager manager].photoPreviewMaxWidth = _photoPreviewMaxWidth;
- }
- - (void)setSelectedAssets:(NSMutableArray *)selectedAssets {
- _selectedAssets = selectedAssets;
- _selectedModels = [NSMutableArray array];
- for (id asset in selectedAssets) {
- TZAssetModel *model = [TZAssetModel modelWithAsset:asset type:TZAssetModelMediaTypePhoto];
- model.isSelected = YES;
- [_selectedModels addObject:model];
- }
- }
- - (void)setAllowPickingImage:(BOOL)allowPickingImage {
- _allowPickingImage = allowPickingImage;
- NSString *allowPickingImageStr = _allowPickingImage ? @"1" : @"0";
- [[NSUserDefaults standardUserDefaults] setObject:allowPickingImageStr forKey:@"tz_allowPickingImage"];
- [[NSUserDefaults standardUserDefaults] synchronize];
- }
- - (void)setAllowPickingVideo:(BOOL)allowPickingVideo {
- _allowPickingVideo = allowPickingVideo;
- NSString *allowPickingVideoStr = _allowPickingVideo ? @"1" : @"0";
- [[NSUserDefaults standardUserDefaults] setObject:allowPickingVideoStr forKey:@"tz_allowPickingVideo"];
- [[NSUserDefaults standardUserDefaults] synchronize];
- }
- - (void)setSortAscendingByModificationDate:(BOOL)sortAscendingByModificationDate {
- _sortAscendingByModificationDate = sortAscendingByModificationDate;
- [TZImageManager manager].sortAscendingByModificationDate = sortAscendingByModificationDate;
- }
- - (void)settingBtnClick {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
- }
- - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
- if (iOS7Later) viewController.automaticallyAdjustsScrollViewInsets = NO;
- if (_timer) { [_timer invalidate]; _timer = nil;}
- [super pushViewController:viewController animated:animated];
- }
- - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
- return UIInterfaceOrientationMaskPortrait;
- }
- #pragma mark - Public
- - (void)cancelButtonClick {
- if (self.autoDismiss) {
- [self dismissViewControllerAnimated:YES completion:^{
- [self callDelegateMethod];
- }];
- } else {
- [self callDelegateMethod];
- }
- }
- - (void)callDelegateMethod {
- // 兼容旧版本
- if ([self.pickerDelegate respondsToSelector:@selector(imagePickerControllerDidCancel:)]) {
- [self.pickerDelegate imagePickerControllerDidCancel:self];
- }
- if ([self.pickerDelegate respondsToSelector:@selector(tz_imagePickerControllerDidCancel:)]) {
- [self.pickerDelegate tz_imagePickerControllerDidCancel:self];
- }
- if (self.imagePickerControllerDidCancelHandle) {
- self.imagePickerControllerDidCancelHandle();
- }
- }
- @end
- @interface TZAlbumPickerController ()<UITableViewDataSource,UITableViewDelegate> {
- UITableView *_tableView;
- }
- @property (nonatomic, strong) NSMutableArray *albumArr;
- @end
- @implementation TZAlbumPickerController
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor whiteColor];
- self.navigationItem.title = [NSBundle tz_localizedStringForKey:@"Photos"];
- TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController;
- self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:imagePickerVc.cancelBtnTitleStr style:UIBarButtonItemStylePlain target:imagePickerVc action:@selector(cancelButtonClick)];
- [self configTableView];
- // 1.6.10 采用微信的方式,只在相册列表页定义backBarButtonItem为返回,其余的顺系统的做法
- self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSBundle tz_localizedStringForKey:@"Back"] style:UIBarButtonItemStylePlain target:nil action:nil];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController;
- [imagePickerVc hideProgressHUD];
- if (_albumArr) {
- for (TZAlbumModel *albumModel in _albumArr) {
- albumModel.selectedModels = imagePickerVc.selectedModels;
- }
- [_tableView reloadData];
- } else {
- [self configTableView];
- }
- if (imagePickerVc.allowTakePicture) {
- self.navigationItem.title = [NSBundle tz_localizedStringForKey:@"Photos"];
- } else if (imagePickerVc.allowPickingVideo) {
- self.navigationItem.title = [NSBundle tz_localizedStringForKey:@"Videos"];
- }
- }
- - (void)configTableView {
- TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController;
- [[TZImageManager manager] getAllAlbums:imagePickerVc.allowPickingVideo allowPickingImage:imagePickerVc.allowPickingImage completion:^(NSArray<TZAlbumModel *> *models) {
- _albumArr = [NSMutableArray arrayWithArray:models];
- for (TZAlbumModel *albumModel in _albumArr) {
- albumModel.selectedModels = imagePickerVc.selectedModels;
- }
- if (!_tableView) {
-
- CGFloat top = kTopHeight;
- CGFloat tableViewHeight = 0;
-
- tableViewHeight = self.view.tz_height - top;
- // if (self.navigationController.navigationBar.isTranslucent) {
- // top = 44;
- // if (iOS7Later) top += 20;
- // tableViewHeight = self.view.tz_height - top;
- // } else {
- // CGFloat navigationHeight = 44;
- // if (iOS7Later) navigationHeight += 20;
- // tableViewHeight = self.view.tz_height - navigationHeight;
- // }
- _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, top, self.view.tz_width, tableViewHeight) style:UITableViewStylePlain];
- _tableView.rowHeight = 70;
- _tableView.tableFooterView = [[UIView alloc] init];
- _tableView.dataSource = self;
- _tableView.delegate = self;
- [_tableView registerClass:[TZAlbumCell class] forCellReuseIdentifier:@"TZAlbumCell"];
- [self.view addSubview:_tableView];
- } else {
- [_tableView reloadData];
- }
- }];
- }
- #pragma mark - UITableViewDataSource && Delegate
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return _albumArr.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- TZAlbumCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TZAlbumCell"];
- TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController;
- cell.selectedCountButton.backgroundColor = imagePickerVc.oKButtonTitleColorNormal;
- cell.model = _albumArr[indexPath.row];
- cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- TZPhotoPickerController *photoPickerVc = [[TZPhotoPickerController alloc] init];
- photoPickerVc.columnNumber = self.columnNumber;
- TZAlbumModel *model = _albumArr[indexPath.row];
- photoPickerVc.model = model;
- __weak typeof(self) weakSelf = self;
- [photoPickerVc setBackButtonClickHandle:^(TZAlbumModel *model) {
- [weakSelf.albumArr replaceObjectAtIndex:indexPath.row withObject:model];
- }];
- [self.navigationController pushViewController:photoPickerVc animated:YES];
- [tableView deselectRowAtIndexPath:indexPath animated:NO];
- }
- #pragma clang diagnostic pop
- @end
- @implementation UIImage (MyBundle)
- + (UIImage *)imageNamedFromMyBundle:(NSString *)name {
- UIImage *image = [UIImage imageNamed:[@"TZImagePickerController.bundle" stringByAppendingPathComponent:name]];
- if (image) {
- return image;
- } else {
- image = [UIImage imageNamed:[@"Frameworks/TZImagePickerController.framework/TZImagePickerController.bundle" stringByAppendingPathComponent:name]];
- if (!image) {
- image = [UIImage imageNamed:name];
- }
- return image;
- }
- }
- @end
|