TZImagePickerController.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. //
  2. // TZImagePickerController.m
  3. // TZImagePickerController
  4. //
  5. // Created by 谭真 on 15/12/24.
  6. // Copyright © 2015年 谭真. All rights reserved.
  7. // version 1.7.8 - 2016.12.20
  8. #import "TZImagePickerController.h"
  9. #import "TZPhotoPickerController.h"
  10. #import "TZPhotoPreviewController.h"
  11. #import "TZAssetModel.h"
  12. #import "TZAssetCell.h"
  13. #import "UIView+Layout.h"
  14. #import "TZImageManager.h"
  15. @interface TZImagePickerController () {
  16. NSTimer *_timer;
  17. UILabel *_tipLabel;
  18. UIButton *_settingBtn;
  19. BOOL _pushPhotoPickerVc;
  20. BOOL _didPushPhotoPickerVc;
  21. UIButton *_progressHUD;
  22. UIView *_HUDContainer;
  23. UIActivityIndicatorView *_HUDIndicatorView;
  24. UILabel *_HUDLabel;
  25. UIStatusBarStyle _originStatusBarStyle;
  26. }
  27. /// Default is 4, Use in photos collectionView in TZPhotoPickerController
  28. /// 默认4列, TZPhotoPickerController中的照片collectionView
  29. @property (nonatomic, assign) NSInteger columnNumber;
  30. @end
  31. @implementation TZImagePickerController
  32. #pragma clang diagnostic push
  33. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. self.view.backgroundColor = [UIColor whiteColor];
  37. self.navigationBar.barStyle = UIBarStyleBlack;
  38. self.navigationBar.translucent = YES;
  39. [TZImageManager manager].shouldFixOrientation = NO;
  40. // Default appearance, you can reset these after this method
  41. // 默认的外观,你可以在这个方法后重置
  42. self.oKButtonTitleColorNormal = [UIColor colorWithRed:(83/255.0) green:(179/255.0) blue:(17/255.0) alpha:1.0];
  43. self.oKButtonTitleColorDisabled = [UIColor colorWithRed:(83/255.0) green:(179/255.0) blue:(17/255.0) alpha:0.5];
  44. if (iOS7Later) {
  45. self.navigationBar.barTintColor = [UIColor colorWithRed:(34/255.0) green:(34/255.0) blue:(34/255.0) alpha:1.0];
  46. self.navigationBar.tintColor = [UIColor whiteColor];
  47. self.automaticallyAdjustsScrollViewInsets = NO;
  48. }
  49. }
  50. - (void)setNaviBgColor:(UIColor *)naviBgColor {
  51. _naviBgColor = naviBgColor;
  52. self.navigationBar.barTintColor = naviBgColor;
  53. }
  54. - (void)setNaviTitleColor:(UIColor *)naviTitleColor {
  55. _naviTitleColor = naviTitleColor;
  56. [self configNaviTitleAppearance];
  57. }
  58. - (void)setNaviTitleFont:(UIFont *)naviTitleFont {
  59. _naviTitleFont = naviTitleFont;
  60. [self configNaviTitleAppearance];
  61. }
  62. - (void)configNaviTitleAppearance {
  63. NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
  64. textAttrs[NSForegroundColorAttributeName] = self.naviTitleColor;
  65. textAttrs[NSFontAttributeName] = self.naviTitleFont;
  66. self.navigationBar.titleTextAttributes = textAttrs;
  67. }
  68. - (void)setBarItemTextFont:(UIFont *)barItemTextFont {
  69. _barItemTextFont = barItemTextFont;
  70. [self configBarButtonItemAppearance];
  71. }
  72. - (void)setBarItemTextColor:(UIColor *)barItemTextColor {
  73. _barItemTextColor = barItemTextColor;
  74. [self configBarButtonItemAppearance];
  75. }
  76. - (void)configBarButtonItemAppearance {
  77. UIBarButtonItem *barItem;
  78. if (iOS9Later) {
  79. barItem = [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[TZImagePickerController class]]];
  80. } else {
  81. barItem = [UIBarButtonItem appearanceWhenContainedIn:[TZImagePickerController class], nil];
  82. }
  83. NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
  84. textAttrs[NSForegroundColorAttributeName] = self.barItemTextColor;
  85. textAttrs[NSFontAttributeName] = self.barItemTextFont;
  86. [barItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
  87. }
  88. - (void)viewWillAppear:(BOOL)animated {
  89. [super viewWillAppear:animated];
  90. _originStatusBarStyle = [UIApplication sharedApplication].statusBarStyle;
  91. [UIApplication sharedApplication].statusBarStyle = iOS7Later ? UIStatusBarStyleLightContent : UIStatusBarStyleBlackOpaque;
  92. }
  93. - (void)viewWillDisappear:(BOOL)animated {
  94. [super viewWillDisappear:animated];
  95. [UIApplication sharedApplication].statusBarStyle = _originStatusBarStyle;
  96. [self hideProgressHUD];
  97. }
  98. - (instancetype)initWithMaxImagesCount:(NSInteger)maxImagesCount delegate:(id<TZImagePickerControllerDelegate>)delegate {
  99. return [self initWithMaxImagesCount:maxImagesCount columnNumber:4 delegate:delegate pushPhotoPickerVc:YES];
  100. }
  101. - (instancetype)initWithMaxImagesCount:(NSInteger)maxImagesCount columnNumber:(NSInteger)columnNumber delegate:(id<TZImagePickerControllerDelegate>)delegate {
  102. return [self initWithMaxImagesCount:maxImagesCount columnNumber:columnNumber delegate:delegate pushPhotoPickerVc:YES];
  103. }
  104. - (instancetype)initWithMaxImagesCount:(NSInteger)maxImagesCount columnNumber:(NSInteger)columnNumber delegate:(id<TZImagePickerControllerDelegate>)delegate pushPhotoPickerVc:(BOOL)pushPhotoPickerVc {
  105. _pushPhotoPickerVc = pushPhotoPickerVc;
  106. TZAlbumPickerController *albumPickerVc = [[TZAlbumPickerController alloc] init];
  107. albumPickerVc.columnNumber = columnNumber;
  108. self = [super initWithRootViewController:albumPickerVc];
  109. if (self) {
  110. self.maxImagesCount = maxImagesCount > 0 ? maxImagesCount : 9; // Default is 9 / 默认最大可选9张图片
  111. self.pickerDelegate = delegate;
  112. self.selectedModels = [NSMutableArray array];
  113. // Allow user picking original photo and video, you also can set No after this method
  114. // 默认准许用户选择原图和视频, 你也可以在这个方法后置为NO
  115. self.allowPickingOriginalPhoto = YES;
  116. self.allowPickingVideo = YES;
  117. self.allowPickingImage = YES;
  118. self.allowTakePicture = YES;
  119. self.sortAscendingByModificationDate = YES;
  120. self.autoDismiss = YES;
  121. self.columnNumber = columnNumber;
  122. [self configDefaultSetting];
  123. if (![[TZImageManager manager] authorizationStatusAuthorized]) {
  124. _tipLabel = [[UILabel alloc] init];
  125. _tipLabel.frame = CGRectMake(8, 120, self.view.tz_width - 16, 60);
  126. _tipLabel.textAlignment = NSTextAlignmentCenter;
  127. _tipLabel.numberOfLines = 0;
  128. _tipLabel.font = [UIFont systemFontOfSize:16];
  129. _tipLabel.textColor = [UIColor blackColor];
  130. NSString *appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleDisplayName"];
  131. if (!appName) appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleName"];
  132. NSString *tipText = [NSString stringWithFormat:[NSBundle tz_localizedStringForKey:@"Allow %@ to access your album in \"Settings -> Privacy -> Photos\""],appName];
  133. _tipLabel.text = tipText;
  134. [self.view addSubview:_tipLabel];
  135. _settingBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  136. [_settingBtn setTitle:self.settingBtnTitleStr forState:UIControlStateNormal];
  137. _settingBtn.frame = CGRectMake(0, 180, self.view.tz_width, 44);
  138. _settingBtn.titleLabel.font = [UIFont systemFontOfSize:18];
  139. [_settingBtn addTarget:self action:@selector(settingBtnClick) forControlEvents:UIControlEventTouchUpInside];
  140. [self.view addSubview:_settingBtn];
  141. _timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(observeAuthrizationStatusChange) userInfo:nil repeats:YES];
  142. } else {
  143. [self pushPhotoPickerVc];
  144. }
  145. }
  146. return self;
  147. }
  148. /// This init method just for previewing photos / 用这个初始化方法以预览图片
  149. - (instancetype)initWithSelectedAssets:(NSMutableArray *)selectedAssets selectedPhotos:(NSMutableArray *)selectedPhotos index:(NSInteger)index{
  150. TZPhotoPreviewController *previewVc = [[TZPhotoPreviewController alloc] init];
  151. self = [super initWithRootViewController:previewVc];
  152. if (self) {
  153. self.selectedAssets = [NSMutableArray arrayWithArray:selectedAssets];
  154. self.allowPickingOriginalPhoto = self.allowPickingOriginalPhoto;
  155. [self configDefaultSetting];
  156. previewVc.photos = [NSMutableArray arrayWithArray:selectedPhotos];
  157. previewVc.currentIndex = index;
  158. __weak typeof(self) weakSelf = self;
  159. [previewVc setDoneButtonClickBlockWithPreviewType:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
  160. [weakSelf dismissViewControllerAnimated:YES completion:^{
  161. if (weakSelf.didFinishPickingPhotosHandle) {
  162. weakSelf.didFinishPickingPhotosHandle(photos,assets,isSelectOriginalPhoto);
  163. }
  164. }];
  165. }];
  166. }
  167. return self;
  168. }
  169. /// This init method for crop photo / 用这个初始化方法以裁剪图片
  170. - (instancetype)initCropTypeWithAsset:(id)asset photo:(UIImage *)photo completion:(void (^)(UIImage *cropImage,id asset))completion {
  171. TZPhotoPreviewController *previewVc = [[TZPhotoPreviewController alloc] init];
  172. self = [super initWithRootViewController:previewVc];
  173. if (self) {
  174. self.maxImagesCount = 1;
  175. self.allowCrop = YES;
  176. self.selectedAssets = [NSMutableArray arrayWithArray:@[asset]];
  177. [self configDefaultSetting];
  178. previewVc.photos = [NSMutableArray arrayWithArray:@[photo]];
  179. previewVc.isCropImage = YES;
  180. previewVc.currentIndex = 0;
  181. __weak typeof(self) weakSelf = self;
  182. [previewVc setDoneButtonClickBlockCropMode:^(UIImage *cropImage, id asset) {
  183. [weakSelf dismissViewControllerAnimated:YES completion:^{
  184. if (completion) {
  185. completion(cropImage,asset);
  186. }
  187. }];
  188. }];
  189. }
  190. return self;
  191. }
  192. - (void)configDefaultSetting {
  193. self.timeout = 15;
  194. self.photoWidth = 828.0;
  195. self.photoPreviewMaxWidth = 600;
  196. self.naviTitleColor = [UIColor whiteColor];
  197. self.naviTitleFont = [UIFont systemFontOfSize:17];
  198. self.barItemTextFont = [UIFont systemFontOfSize:15];
  199. self.barItemTextColor = [UIColor whiteColor];
  200. self.allowPreview = YES;
  201. [self configDefaultImageName];
  202. [self configDefaultBtnTitle];
  203. }
  204. - (void)configDefaultImageName {
  205. self.takePictureImageName = @"takePicture.png";
  206. self.photoSelImageName = @"photo_sel_photoPickerVc.png";
  207. self.photoDefImageName = @"photo_def_photoPickerVc.png";
  208. self.photoNumberIconImageName = @"photo_number_icon.png";
  209. self.photoPreviewOriginDefImageName = @"preview_original_def.png";
  210. self.photoOriginDefImageName = @"photo_original_def.png";
  211. self.photoOriginSelImageName = @"photo_original_sel.png";
  212. }
  213. - (void)configDefaultBtnTitle {
  214. self.doneBtnTitleStr = [NSBundle tz_localizedStringForKey:@"Done"];
  215. self.cancelBtnTitleStr = [NSBundle tz_localizedStringForKey:@"Cancel"];
  216. self.previewBtnTitleStr = [NSBundle tz_localizedStringForKey:@"Preview"];
  217. self.fullImageBtnTitleStr = [NSBundle tz_localizedStringForKey:@"Full image"];
  218. self.settingBtnTitleStr = [NSBundle tz_localizedStringForKey:@"Setting"];
  219. self.processHintStr = [NSBundle tz_localizedStringForKey:@"Processing..."];
  220. }
  221. - (void)observeAuthrizationStatusChange {
  222. if ([[TZImageManager manager] authorizationStatusAuthorized]) {
  223. [_tipLabel removeFromSuperview];
  224. [_settingBtn removeFromSuperview];
  225. [_timer invalidate];
  226. _timer = nil;
  227. [self pushPhotoPickerVc];
  228. }
  229. }
  230. - (void)pushPhotoPickerVc {
  231. _didPushPhotoPickerVc = NO;
  232. // 1.6.8 判断是否需要push到照片选择页,如果_pushPhotoPickerVc为NO,则不push
  233. if (!_didPushPhotoPickerVc && _pushPhotoPickerVc) {
  234. TZPhotoPickerController *photoPickerVc = [[TZPhotoPickerController alloc] init];
  235. photoPickerVc.isFirstAppear = YES;
  236. photoPickerVc.columnNumber = self.columnNumber;
  237. [[TZImageManager manager] getCameraRollAlbum:self.allowPickingVideo allowPickingImage:self.allowPickingImage completion:^(TZAlbumModel *model) {
  238. photoPickerVc.model = model;
  239. [self pushViewController:photoPickerVc animated:YES];
  240. _didPushPhotoPickerVc = YES;
  241. }];
  242. }
  243. }
  244. - (void)showAlertWithTitle:(NSString *)title {
  245. if (iOS8Later) {
  246. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
  247. [alertController addAction:[UIAlertAction actionWithTitle:[NSBundle tz_localizedStringForKey:@"OK"] style:UIAlertActionStyleDefault handler:nil]];
  248. [self presentViewController:alertController animated:YES completion:nil];
  249. } else {
  250. [[[UIAlertView alloc] initWithTitle:title message:nil delegate:nil cancelButtonTitle:[NSBundle tz_localizedStringForKey:@"OK"] otherButtonTitles:nil, nil] show];
  251. }
  252. }
  253. - (void)showProgressHUD {
  254. if (!_progressHUD) {
  255. _progressHUD = [UIButton buttonWithType:UIButtonTypeCustom];
  256. [_progressHUD setBackgroundColor:[UIColor clearColor]];
  257. _HUDContainer = [[UIView alloc] init];
  258. _HUDContainer.frame = CGRectMake((self.view.tz_width - 120) / 2, (self.view.tz_height - 90) / 2, 120, 90);
  259. _HUDContainer.layer.cornerRadius = 8;
  260. _HUDContainer.clipsToBounds = YES;
  261. _HUDContainer.backgroundColor = [UIColor darkGrayColor];
  262. _HUDContainer.alpha = 0.7;
  263. _HUDIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
  264. _HUDIndicatorView.frame = CGRectMake(45, 15, 30, 30);
  265. _HUDLabel = [[UILabel alloc] init];
  266. _HUDLabel.frame = CGRectMake(0,40, 120, 50);
  267. _HUDLabel.textAlignment = NSTextAlignmentCenter;
  268. _HUDLabel.text = self.processHintStr;
  269. _HUDLabel.font = [UIFont systemFontOfSize:15];
  270. _HUDLabel.textColor = [UIColor whiteColor];
  271. [_HUDContainer addSubview:_HUDLabel];
  272. [_HUDContainer addSubview:_HUDIndicatorView];
  273. [_progressHUD addSubview:_HUDContainer];
  274. }
  275. [_HUDIndicatorView startAnimating];
  276. [[UIApplication sharedApplication].keyWindow addSubview:_progressHUD];
  277. // if over time, dismiss HUD automatic
  278. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.timeout * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  279. [self hideProgressHUD];
  280. });
  281. }
  282. - (void)hideProgressHUD {
  283. if (_progressHUD) {
  284. [_HUDIndicatorView stopAnimating];
  285. [_progressHUD removeFromSuperview];
  286. }
  287. }
  288. - (void)setMaxImagesCount:(NSInteger)maxImagesCount {
  289. _maxImagesCount = maxImagesCount;
  290. if (maxImagesCount > 1) {
  291. _showSelectBtn = YES;
  292. _allowCrop = NO;
  293. }
  294. }
  295. - (void)setShowSelectBtn:(BOOL)showSelectBtn {
  296. _showSelectBtn = showSelectBtn;
  297. // 多选模式下,不允许让showSelectBtn为NO
  298. if (!showSelectBtn && _maxImagesCount > 1) {
  299. _showSelectBtn = YES;
  300. }
  301. }
  302. - (void)setAllowCrop:(BOOL)allowCrop {
  303. _allowCrop = _maxImagesCount > 1 ? NO : allowCrop;
  304. if (allowCrop) { // 允许裁剪的时候,不能选原图和GIF
  305. self.allowPickingOriginalPhoto = NO;
  306. self.allowPickingGif = NO;
  307. }
  308. }
  309. - (void)setCircleCropRadius:(NSInteger)circleCropRadius {
  310. _circleCropRadius = circleCropRadius;
  311. _cropRect = CGRectMake(self.view.tz_width / 2 - circleCropRadius, self.view.tz_height / 2 - _circleCropRadius, _circleCropRadius * 2, _circleCropRadius * 2);
  312. }
  313. - (CGRect)cropRect {
  314. if (_cropRect.size.width > 0) {
  315. return _cropRect;
  316. }
  317. CGFloat cropViewWH = self.view.tz_width;
  318. return CGRectMake(0, (self.view.tz_height - self.view.tz_width) / 2, cropViewWH, cropViewWH);
  319. }
  320. - (void)setTimeout:(NSInteger)timeout {
  321. _timeout = timeout;
  322. if (timeout < 5) {
  323. _timeout = 5;
  324. } else if (_timeout > 60) {
  325. _timeout = 60;
  326. }
  327. }
  328. - (void)setColumnNumber:(NSInteger)columnNumber {
  329. _columnNumber = columnNumber;
  330. if (columnNumber <= 2) {
  331. _columnNumber = 2;
  332. } else if (columnNumber >= 6) {
  333. _columnNumber = 6;
  334. }
  335. TZAlbumPickerController *albumPickerVc = [self.childViewControllers firstObject];
  336. albumPickerVc.columnNumber = _columnNumber;
  337. [TZImageManager manager].columnNumber = _columnNumber;
  338. }
  339. - (void)setMinPhotoWidthSelectable:(NSInteger)minPhotoWidthSelectable {
  340. _minPhotoWidthSelectable = minPhotoWidthSelectable;
  341. [TZImageManager manager].minPhotoWidthSelectable = minPhotoWidthSelectable;
  342. }
  343. - (void)setMinPhotoHeightSelectable:(NSInteger)minPhotoHeightSelectable {
  344. _minPhotoHeightSelectable = minPhotoHeightSelectable;
  345. [TZImageManager manager].minPhotoHeightSelectable = minPhotoHeightSelectable;
  346. }
  347. - (void)setHideWhenCanNotSelect:(BOOL)hideWhenCanNotSelect {
  348. _hideWhenCanNotSelect = hideWhenCanNotSelect;
  349. [TZImageManager manager].hideWhenCanNotSelect = hideWhenCanNotSelect;
  350. }
  351. - (void)setPhotoPreviewMaxWidth:(CGFloat)photoPreviewMaxWidth {
  352. _photoPreviewMaxWidth = photoPreviewMaxWidth;
  353. if (photoPreviewMaxWidth > 800) {
  354. _photoPreviewMaxWidth = 800;
  355. } else if (photoPreviewMaxWidth < 500) {
  356. _photoPreviewMaxWidth = 500;
  357. }
  358. [TZImageManager manager].photoPreviewMaxWidth = _photoPreviewMaxWidth;
  359. }
  360. - (void)setSelectedAssets:(NSMutableArray *)selectedAssets {
  361. _selectedAssets = selectedAssets;
  362. _selectedModels = [NSMutableArray array];
  363. for (id asset in selectedAssets) {
  364. TZAssetModel *model = [TZAssetModel modelWithAsset:asset type:TZAssetModelMediaTypePhoto];
  365. model.isSelected = YES;
  366. [_selectedModels addObject:model];
  367. }
  368. }
  369. - (void)setAllowPickingImage:(BOOL)allowPickingImage {
  370. _allowPickingImage = allowPickingImage;
  371. NSString *allowPickingImageStr = _allowPickingImage ? @"1" : @"0";
  372. [[NSUserDefaults standardUserDefaults] setObject:allowPickingImageStr forKey:@"tz_allowPickingImage"];
  373. [[NSUserDefaults standardUserDefaults] synchronize];
  374. }
  375. - (void)setAllowPickingVideo:(BOOL)allowPickingVideo {
  376. _allowPickingVideo = allowPickingVideo;
  377. NSString *allowPickingVideoStr = _allowPickingVideo ? @"1" : @"0";
  378. [[NSUserDefaults standardUserDefaults] setObject:allowPickingVideoStr forKey:@"tz_allowPickingVideo"];
  379. [[NSUserDefaults standardUserDefaults] synchronize];
  380. }
  381. - (void)setSortAscendingByModificationDate:(BOOL)sortAscendingByModificationDate {
  382. _sortAscendingByModificationDate = sortAscendingByModificationDate;
  383. [TZImageManager manager].sortAscendingByModificationDate = sortAscendingByModificationDate;
  384. }
  385. - (void)settingBtnClick {
  386. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
  387. }
  388. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
  389. if (iOS7Later) viewController.automaticallyAdjustsScrollViewInsets = NO;
  390. if (_timer) { [_timer invalidate]; _timer = nil;}
  391. [super pushViewController:viewController animated:animated];
  392. }
  393. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  394. return UIInterfaceOrientationMaskPortrait;
  395. }
  396. #pragma mark - Public
  397. - (void)cancelButtonClick {
  398. if (self.autoDismiss) {
  399. [self dismissViewControllerAnimated:YES completion:^{
  400. [self callDelegateMethod];
  401. }];
  402. } else {
  403. [self callDelegateMethod];
  404. }
  405. }
  406. - (void)callDelegateMethod {
  407. // 兼容旧版本
  408. if ([self.pickerDelegate respondsToSelector:@selector(imagePickerControllerDidCancel:)]) {
  409. [self.pickerDelegate imagePickerControllerDidCancel:self];
  410. }
  411. if ([self.pickerDelegate respondsToSelector:@selector(tz_imagePickerControllerDidCancel:)]) {
  412. [self.pickerDelegate tz_imagePickerControllerDidCancel:self];
  413. }
  414. if (self.imagePickerControllerDidCancelHandle) {
  415. self.imagePickerControllerDidCancelHandle();
  416. }
  417. }
  418. @end
  419. @interface TZAlbumPickerController ()<UITableViewDataSource,UITableViewDelegate> {
  420. UITableView *_tableView;
  421. }
  422. @property (nonatomic, strong) NSMutableArray *albumArr;
  423. @end
  424. @implementation TZAlbumPickerController
  425. - (void)viewDidLoad {
  426. [super viewDidLoad];
  427. self.view.backgroundColor = [UIColor whiteColor];
  428. self.navigationItem.title = [NSBundle tz_localizedStringForKey:@"Photos"];
  429. TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController;
  430. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:imagePickerVc.cancelBtnTitleStr style:UIBarButtonItemStylePlain target:imagePickerVc action:@selector(cancelButtonClick)];
  431. [self configTableView];
  432. // 1.6.10 采用微信的方式,只在相册列表页定义backBarButtonItem为返回,其余的顺系统的做法
  433. self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSBundle tz_localizedStringForKey:@"Back"] style:UIBarButtonItemStylePlain target:nil action:nil];
  434. }
  435. - (void)viewWillAppear:(BOOL)animated {
  436. [super viewWillAppear:animated];
  437. TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController;
  438. [imagePickerVc hideProgressHUD];
  439. if (_albumArr) {
  440. for (TZAlbumModel *albumModel in _albumArr) {
  441. albumModel.selectedModels = imagePickerVc.selectedModels;
  442. }
  443. [_tableView reloadData];
  444. } else {
  445. [self configTableView];
  446. }
  447. if (imagePickerVc.allowTakePicture) {
  448. self.navigationItem.title = [NSBundle tz_localizedStringForKey:@"Photos"];
  449. } else if (imagePickerVc.allowPickingVideo) {
  450. self.navigationItem.title = [NSBundle tz_localizedStringForKey:@"Videos"];
  451. }
  452. }
  453. - (void)configTableView {
  454. TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController;
  455. [[TZImageManager manager] getAllAlbums:imagePickerVc.allowPickingVideo allowPickingImage:imagePickerVc.allowPickingImage completion:^(NSArray<TZAlbumModel *> *models) {
  456. _albumArr = [NSMutableArray arrayWithArray:models];
  457. for (TZAlbumModel *albumModel in _albumArr) {
  458. albumModel.selectedModels = imagePickerVc.selectedModels;
  459. }
  460. if (!_tableView) {
  461. CGFloat top = kTopHeight;
  462. CGFloat tableViewHeight = 0;
  463. tableViewHeight = self.view.tz_height - top;
  464. // if (self.navigationController.navigationBar.isTranslucent) {
  465. // top = 44;
  466. // if (iOS7Later) top += 20;
  467. // tableViewHeight = self.view.tz_height - top;
  468. // } else {
  469. // CGFloat navigationHeight = 44;
  470. // if (iOS7Later) navigationHeight += 20;
  471. // tableViewHeight = self.view.tz_height - navigationHeight;
  472. // }
  473. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, top, self.view.tz_width, tableViewHeight) style:UITableViewStylePlain];
  474. _tableView.rowHeight = 70;
  475. _tableView.tableFooterView = [[UIView alloc] init];
  476. _tableView.dataSource = self;
  477. _tableView.delegate = self;
  478. [_tableView registerClass:[TZAlbumCell class] forCellReuseIdentifier:@"TZAlbumCell"];
  479. [self.view addSubview:_tableView];
  480. } else {
  481. [_tableView reloadData];
  482. }
  483. }];
  484. }
  485. #pragma mark - UITableViewDataSource && Delegate
  486. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  487. return _albumArr.count;
  488. }
  489. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  490. TZAlbumCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TZAlbumCell"];
  491. TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController;
  492. cell.selectedCountButton.backgroundColor = imagePickerVc.oKButtonTitleColorNormal;
  493. cell.model = _albumArr[indexPath.row];
  494. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  495. return cell;
  496. }
  497. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  498. TZPhotoPickerController *photoPickerVc = [[TZPhotoPickerController alloc] init];
  499. photoPickerVc.columnNumber = self.columnNumber;
  500. TZAlbumModel *model = _albumArr[indexPath.row];
  501. photoPickerVc.model = model;
  502. __weak typeof(self) weakSelf = self;
  503. [photoPickerVc setBackButtonClickHandle:^(TZAlbumModel *model) {
  504. [weakSelf.albumArr replaceObjectAtIndex:indexPath.row withObject:model];
  505. }];
  506. [self.navigationController pushViewController:photoPickerVc animated:YES];
  507. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  508. }
  509. #pragma clang diagnostic pop
  510. @end
  511. @implementation UIImage (MyBundle)
  512. + (UIImage *)imageNamedFromMyBundle:(NSString *)name {
  513. UIImage *image = [UIImage imageNamed:[@"TZImagePickerController.bundle" stringByAppendingPathComponent:name]];
  514. if (image) {
  515. return image;
  516. } else {
  517. image = [UIImage imageNamed:[@"Frameworks/TZImagePickerController.framework/TZImagePickerController.bundle" stringByAppendingPathComponent:name]];
  518. if (!image) {
  519. image = [UIImage imageNamed:name];
  520. }
  521. return image;
  522. }
  523. }
  524. @end