QMPickedPhotoViewController.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // QMPickedPhotoViewController.m
  3. // IMSDK-OC
  4. //
  5. // Created by HCF on 16/8/11.
  6. // Copyright © 2016年 HCF. All rights reserved.
  7. //
  8. #import "QMPickedPhotoViewController.h"
  9. #import "QMFileCollectionCell.h"
  10. #import <Photos/Photos.h>
  11. #import "QMFileTabbarView.h"
  12. #import "QMChatRoomViewController.h"
  13. @interface QMPickedPhotoViewController ()<UICollectionViewDelegate, UICollectionViewDataSource> {
  14. UICollectionView *_collectionView;
  15. PHFetchResult *_photoAssets;
  16. PHCachingImageManager *_cacheManager;
  17. QMFileTabbarView *_tabbarView;
  18. CGFloat _navHeight;
  19. }
  20. @property (nonatomic, strong) NSMutableSet *pickedImageSet;
  21. @end
  22. @implementation QMPickedPhotoViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. CGRect StatusRect = [[UIApplication sharedApplication] statusBarFrame];
  26. CGRect NavRect = self.navigationController.navigationBar.frame;
  27. _navHeight = StatusRect.size.height + NavRect.size.height;
  28. _cacheManager = [[PHCachingImageManager alloc] init];
  29. self.pickedImageSet = [[NSMutableSet alloc] init];
  30. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  31. layout.itemSize = CGSizeMake((kScreenWidth-6)/3, (kScreenWidth-6)/3);
  32. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  33. layout.sectionInset = UIEdgeInsetsMake(0, 1, 0, 1);
  34. layout.minimumLineSpacing = 2.0;
  35. layout.minimumInteritemSpacing = 1.0;
  36. layout.headerReferenceSize = CGSizeMake(0, 0);
  37. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-_navHeight-44) collectionViewLayout:layout];
  38. _collectionView.backgroundColor = [UIColor whiteColor];
  39. _collectionView.delegate = self;
  40. _collectionView.dataSource = self;
  41. [self.view addSubview:_collectionView];
  42. [_collectionView registerClass:[QMFileCollectionCell self] forCellWithReuseIdentifier:NSStringFromClass(QMFileCollectionCell.self)];
  43. _tabbarView = [[QMFileTabbarView alloc] init];
  44. _tabbarView.frame = CGRectMake(0, kScreenHeight-44-_navHeight, kScreenWidth, 44);
  45. [self.view addSubview:_tabbarView];
  46. __weak QMPickedPhotoViewController *strongSelf = self;
  47. _tabbarView.selectAction = ^{
  48. QMChatRoomViewController * tagViewController = nil;
  49. for (UIViewController *viewController in strongSelf.navigationController.viewControllers) {
  50. if ([viewController isKindOfClass:[QMChatRoomViewController class]]) {
  51. tagViewController = (QMChatRoomViewController *)viewController;
  52. [strongSelf.navigationController popToViewController:tagViewController animated:true];
  53. for (PHAsset *asset in strongSelf.pickedImageSet) {
  54. [[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  55. NSString *photoPath = [[info objectForKey:@"PHImageFileURLKey"] absoluteString];
  56. NSArray *array = [photoPath componentsSeparatedByString:@"/"];
  57. NSString *fileSize = [imageData length]<1024*1024 ? [NSString stringWithFormat:@"%d KB", (int)([imageData length]/1024.0)] : [NSString stringWithFormat:@"%d MB", (int)([imageData length]/1024.0/1024)];
  58. NSString * filePath = [NSString stringWithFormat:@"%@/%@", NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0], array.lastObject];
  59. // 写入sendVideo文件夹
  60. [imageData writeToFile:filePath atomically:YES];
  61. [tagViewController sendFileMessageWithName:array.lastObject AndSize:fileSize AndPath:array.lastObject];
  62. }];
  63. }
  64. }
  65. }
  66. };
  67. dispatch_async(dispatch_get_main_queue(), ^{
  68. PHFetchOptions *options = [[PHFetchOptions alloc] init];
  69. options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
  70. _photoAssets = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:options];
  71. [_collectionView reloadData];
  72. });
  73. }
  74. - (void)dealloc {
  75. }
  76. #pragma mark - CollectionViewDelegate CollectionViewDataSource
  77. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  78. return _photoAssets ? _photoAssets.count : 0;
  79. }
  80. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  81. QMFileCollectionCell * cell = (QMFileCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(QMFileCollectionCell.self) forIndexPath:indexPath];
  82. return cell;
  83. }
  84. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  85. if ([cell isKindOfClass:[QMFileCollectionCell class]]) {
  86. QMFileCollectionCell *displayCell = (QMFileCollectionCell *)cell;
  87. displayCell.imageManager = _cacheManager;
  88. if (_photoAssets) {
  89. PHAsset * asset = _photoAssets[indexPath.item];
  90. displayCell.imageAsset = asset;
  91. displayCell.pickedItemImageView.hidden = ![self.pickedImageSet containsObject:asset];
  92. }
  93. }
  94. }
  95. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  96. if (_photoAssets) {
  97. PHAsset *asset = _photoAssets[indexPath.item];
  98. if ([self.pickedImageSet containsObject:asset]) {
  99. [self.pickedImageSet removeObject:asset];
  100. }else {
  101. if (self.pickedImageSet.count>0) {
  102. return;
  103. }
  104. [self.pickedImageSet addObject:asset];
  105. }
  106. QMFileCollectionCell * cell = (QMFileCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
  107. cell.pickedItemImageView.hidden = ![self.pickedImageSet containsObject:asset];
  108. }
  109. if (self.pickedImageSet.count>0) {
  110. _tabbarView.doneButton.selected = YES;
  111. }else {
  112. _tabbarView.doneButton.selected = NO;
  113. }
  114. }
  115. - (void)didReceiveMemoryWarning {
  116. [super didReceiveMemoryWarning];
  117. // Dispose of any resources that can be recreated.
  118. }
  119. @end