RITLPhotosGroupTableViewController.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // RITLPhotosGroupTableViewController.m
  3. // RITLPhotoDemo
  4. //
  5. // Created by YueWen on 2018/3/7.
  6. // Copyright © 2018年 YueWen. All rights reserved.
  7. //
  8. #import "RITLPhotosGroupTableViewController.h"
  9. #import "RITLPhotosGroupCell.h"
  10. #import "PHAssetCollection+RITLPhotos.h"
  11. #import "NSBundle+RITLPhotos.h"
  12. #import "PHPhotoLibrary+RITLPhotoStore.h"
  13. #import "RITLPhotosCollectionViewController.h"
  14. #import "RITLKit.h"
  15. //static NSString *const RITLGroupTableViewControllerPlaceHolderImageName = @"RITLPhotos.bundle/ritl_placeholder";
  16. @interface RITLPhotosGroupTableViewController (AssetData)
  17. /// 加载默认的组
  18. - (void)loadDefaultAblumGroups;
  19. @end
  20. @interface RITLPhotosGroupTableViewController (RITLString)
  21. - (NSAttributedString *)titleForCollection:(PHAssetCollection *)collection count:(NSInteger)count;
  22. @end
  23. @interface RITLPhotosGroupTableViewController (RITLPhotosCollectionViewController)
  24. - (void)pushPhotosCollectionViewController:(NSIndexPath *)indexPath animated:(BOOL)animated;
  25. @end
  26. @interface RITLPhotosGroupTableViewController ()
  27. @property (nonatomic, copy)NSArray <PHAssetCollection *> *groups;
  28. @end
  29. @implementation RITLPhotosGroupTableViewController
  30. - (void)viewDidLoad {
  31. [super viewDidLoad];
  32. //navigationItem
  33. self.navigationItem.title = Localized(@"JX_Photo");
  34. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:Localized(@"JX_Cencal") style:UIBarButtonItemStyleDone target:self action:@selector(dismissPhotoControllers)];
  35. //data
  36. self.groups = @[];
  37. //tableView
  38. self.tableView.tableFooterView = [[UIView alloc]init];
  39. //cell
  40. [self.tableView registerClass:RITLPhotosGroupCell.class forCellReuseIdentifier:@"group"];
  41. }
  42. - (void)viewDidAppear:(BOOL)animated
  43. {
  44. [super viewDidAppear:animated];
  45. if (self.groups.count == 0) {
  46. [self loadDefaultAblumGroups];
  47. }
  48. }
  49. - (void)didReceiveMemoryWarning {
  50. [super didReceiveMemoryWarning];
  51. // Dispose of any resources that can be recreated.
  52. }
  53. #pragma mark - Dismiss
  54. - (void)dismissPhotoControllers
  55. {
  56. if(self.navigationController.presentingViewController){//如果是模态弹出
  57. [self.navigationController dismissViewControllerAnimated:true completion:nil];
  58. }else if(self.navigationController){
  59. [self.navigationController popViewControllerAnimated:true];
  60. }
  61. }
  62. #pragma mark - Table view data source
  63. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  64. {
  65. return 1;
  66. }
  67. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  68. {
  69. return self.groups.count;
  70. }
  71. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  72. RITLPhotosGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:@"group" forIndexPath:indexPath];
  73. // Configure the cell...
  74. PHAssetCollection *collection = self.groups[indexPath.row];
  75. [collection ritl_headerImageWithSize:CGSizeMake(30, 30) mode:PHImageRequestOptionsDeliveryModeOpportunistic complete:^(NSString * _Nonnull title, NSUInteger count, UIImage * _Nullable image) {
  76. //set value
  77. cell.titleLabel.attributedText = [self titleForCollection:collection count:count];
  78. cell.imageView.image = count > 0 ? image : /*RITLGroupTableViewControllerPlaceHolderImageName.ritl_image*/NSBundle.ritl_placeholder;
  79. }];
  80. return cell;
  81. }
  82. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  83. {
  84. return 60;
  85. }
  86. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  87. {
  88. [self pushPhotosCollectionViewController:indexPath animated:true];
  89. }
  90. @end
  91. @implementation RITLPhotosGroupTableViewController (AssetData)
  92. - (void)loadDefaultAblumGroups
  93. {
  94. [PHPhotoLibrary.sharedPhotoLibrary fetchAlbumRegularGroupsByUserLibrary:^(NSArray<PHAssetCollection *> * _Nonnull groups) {
  95. //set
  96. self.groups = groups;
  97. if (NSThread.isMainThread) {
  98. [self.tableView reloadData];//reload
  99. }else {
  100. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:false];
  101. }
  102. }];
  103. }
  104. @end
  105. @implementation RITLPhotosGroupTableViewController (RITLString)
  106. - (NSAttributedString *)titleForCollection:(PHAssetCollection *)collection count:(NSInteger)count
  107. {
  108. //数据拼接
  109. NSMutableAttributedString *result = [[NSMutableAttributedString alloc]
  110. initWithString:collection.localizedTitle
  111. attributes:@{NSFontAttributeName: RITLUtilityFont(RITLFontPingFangSC_Medium, 15)}];
  112. if (count < 0 || count == NSNotFound) {
  113. count = 0;
  114. }
  115. //
  116. NSString *countString = [NSString stringWithFormat:@" (%@)",@(count).stringValue];
  117. //数量
  118. NSMutableAttributedString *countResult = [[NSMutableAttributedString alloc]
  119. initWithString:countString
  120. attributes:@{NSFontAttributeName: RITLUtilityFont(RITLFontPingFangSC_Medium, 15),NSForegroundColorAttributeName:RITLColorSimpleFromIntRBG(102)}];
  121. [result appendAttributedString:countResult];
  122. return result;
  123. }
  124. @end
  125. @implementation RITLPhotosGroupTableViewController (RITLPhotosCollectionViewController)
  126. - (void)pushPhotosCollectionViewController:(NSIndexPath *)indexPath animated:(BOOL)animated
  127. {
  128. PHAssetCollection *collection = self.groups[indexPath.row];
  129. [self.navigationController pushViewController:({
  130. RITLPhotosCollectionViewController *collectionViewController = RITLPhotosCollectionViewController.photosCollectionController;
  131. //设置
  132. collectionViewController.navigationItem.title = NSLocalizedString(collection.localizedTitle, @"");
  133. collectionViewController.localIdentifier = collection.localIdentifier;
  134. collectionViewController;
  135. }) animated:animated];
  136. }
  137. @end