DKPhotoPickerGroupViewController.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // DKPhotoPickerGroupViewController.m
  3. // DKAssetsPickerDemo
  4. // Copyright (c) 2014年 com.zixue101.www. All rights reserved.
  5. //
  6. #define CELL_ROW 4
  7. #define CELL_MARGIN 5
  8. #define CELL_LINE_MARGIN 5
  9. #import "DKPhotoPickerGroupViewController.h"
  10. #import "DKPhotoPickerCollectionView.h"
  11. #import "DKPhotoPickerDatas.h"
  12. #import "DKPhotoPickerGroupViewController.h"
  13. #import "DKPhotoPickerGroup.h"
  14. #import "DKPhotoPickerGroupTableViewCell.h"
  15. #import "DKPhotoPickerAssetsViewController.h"
  16. #import <AssetsLibrary/AssetsLibrary.h>
  17. @interface DKPhotoPickerGroupViewController () <UITableViewDataSource,UITableViewDelegate>
  18. @property (nonatomic , weak) DKPhotoPickerAssetsViewController *collectionVc;
  19. @property (nonatomic , weak) UITableView *tableView;
  20. @property (nonatomic , strong) NSArray *groups;
  21. @end
  22. @implementation DKPhotoPickerGroupViewController
  23. - (UITableView *)tableView{
  24. if (!_tableView) {
  25. UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  26. tableView.translatesAutoresizingMaskIntoConstraints = NO;
  27. tableView.delegate = self;
  28. [self.view addSubview:tableView];
  29. self.tableView = tableView;
  30. NSDictionary *views = NSDictionaryOfVariableBindings(tableView);
  31. NSString *heightVfl = @"V:|-0-[tableView]-0-|";
  32. [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:heightVfl options:0 metrics:nil views:views]];
  33. NSString *widthVfl = @"H:|-0-[tableView]-0-|";
  34. [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:widthVfl options:0 metrics:nil views:views]];
  35. }
  36. return _tableView;
  37. }
  38. - (void)viewDidLoad {
  39. [super viewDidLoad];
  40. [self tableView];
  41. // 设置按钮
  42. [self setupButtons];
  43. // 获取图片
  44. [self getImgs];
  45. self.title = @"选择相册";
  46. }
  47. - (void) setupButtons{
  48. UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStyleDone target:self action:@selector(back)];
  49. self.navigationItem.rightBarButtonItem = barItem;
  50. }
  51. #pragma mark - <UITableViewDataSource>
  52. - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  53. return self.groups.count;
  54. }
  55. - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  56. DKPhotoPickerGroupTableViewCell *cell = [DKPhotoPickerGroupTableViewCell instanceCell];
  57. cell.group = self.groups[indexPath.row];
  58. return cell;
  59. }
  60. #pragma mark 跳转到控制器里面的内容
  61. - (void) jump2StatusVc{
  62. // 如果是相册
  63. DKPhotoPickerGroup *gp = nil;
  64. for (DKPhotoPickerGroup *group in self.groups) {
  65. if ((self.status == PickerViewShowStatusCameraRoll || self.status == PickerViewShowStatusVideo) && ([group.groupName isEqualToString:@"Camera Roll"] || [group.groupName isEqualToString:@"相机胶卷"])) {
  66. gp = group;
  67. break;
  68. }else if (self.status == PickerViewShowStatusSavePhotos && ([group.groupName isEqualToString:@"Saved Photos"] || [group.groupName isEqualToString:@"保存相册"])){
  69. gp = group;
  70. break;
  71. }else if (self.status == PickerViewShowStatusPhotoStream && ([group.groupName isEqualToString:@"Stream"] || [group.groupName isEqualToString:@"我的照片流"])){
  72. gp = group;
  73. break;
  74. }
  75. }
  76. if (!gp) return ;
  77. DKPhotoPickerAssetsViewController *assetsVc = [[DKPhotoPickerAssetsViewController alloc] init];
  78. assetsVc.cameraCount=self.cameraCount;
  79. assetsVc.selectPickerAssets = self.selectAsstes;
  80. assetsVc.assetsGroup = gp;
  81. assetsVc.groupVc = self;
  82. assetsVc.minCount = self.minCount;
  83. [self.navigationController pushViewController:assetsVc animated:NO];
  84. }
  85. #pragma mark -<UITableViewDelegate>
  86. - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  87. return 80;
  88. }
  89. - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  90. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  91. DKPhotoPickerGroup *group = self.groups[indexPath.row];
  92. DKPhotoPickerAssetsViewController *assetsVc = [[DKPhotoPickerAssetsViewController alloc] init];
  93. assetsVc.cameraCount=self.cameraCount;
  94. assetsVc.selectPickerAssets = self.selectAsstes;
  95. assetsVc.groupVc = self;
  96. assetsVc.assetsGroup = group;
  97. assetsVc.minCount = self.minCount;
  98. [self.navigationController pushViewController:assetsVc animated:YES];
  99. }
  100. #pragma mark -<Images Datas>
  101. -(void)getImgs{
  102. DKPhotoPickerDatas *datas = [DKPhotoPickerDatas defaultPicker];
  103. __weak typeof(self) weakSelf = self;
  104. if (self.status == PickerViewShowStatusVideo){
  105. // 获取所有的图片URLs
  106. [datas getAllGroupWithVideos:^(NSArray *groups) {
  107. self.groups = groups;
  108. if (self.status) {
  109. [self jump2StatusVc];
  110. }
  111. weakSelf.tableView.dataSource = self;
  112. [weakSelf.tableView reloadData];
  113. }];
  114. }else{
  115. // 获取所有的图片URLs
  116. [datas getAllGroupWithPhotos:^(NSArray *groups) {
  117. self.groups = groups;
  118. if (self.status) {
  119. [self jump2StatusVc];
  120. }
  121. weakSelf.tableView.dataSource = self;
  122. [weakSelf.tableView reloadData];
  123. }];
  124. }
  125. }
  126. #pragma mark -<Navigation Actions>
  127. - (void) back{
  128. [self dismissViewControllerAnimated:YES completion:nil];
  129. }
  130. @end