AttachmentViewController.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // AttachmentViewController.m
  3. // IBOSS
  4. //
  5. // Created by 关宏厚 on 2018/11/15.
  6. // Copyright © 2018 elongtian. All rights reserved.
  7. //
  8. #import "AttachmentViewController.h"
  9. @interface AttachmentViewController ()
  10. @end
  11. @implementation AttachmentViewController
  12. #pragma mark - 公共函数
  13. - (void)viewDidLoad {
  14. [super viewDidLoad];
  15. [self.view setBackgroundColor:[UIColor whiteColor]];
  16. [self initUI];
  17. }
  18. #pragma mark - 委托函数
  19. /**
  20. 相册collectionview每个分区的项数
  21. @param collectionView
  22. @param section
  23. @return
  24. */
  25. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  26. return [_attachImageArray count];
  27. }
  28. /**
  29. 相册加载cellectionviewcell
  30. @param collectionView
  31. @param indexPath
  32. @return
  33. */
  34. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  35. AlbumPhotoCollectionViewCell *cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:PhotoCollectionViewCellIdentifier forIndexPath:indexPath];
  36. cell1.indexPath = indexPath;
  37. ImageModel *image = _attachImageArray[indexPath.row];
  38. NSString *imagepath = [image imageName];
  39. imagepath= [self returnFormatString:imagepath];
  40. if(imagepath != nil&&[imagepath length]>0){
  41. NSString *imageUrl= [NSString stringWithFormat:@"http://%@:%@/WebService/%@",kkServerUrl,kkServerPort,imagepath];
  42. NSURL *url= [NSURL URLWithString:[imageUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]];
  43. [cell1.photoImageView downloadImageWithURL:url];
  44. }
  45. return cell1;
  46. }
  47. #pragma mark - 私有函数
  48. -(void)loadData:(NSMutableArray*)imageArray{
  49. _attachImageArray=imageArray;
  50. _sharePhotoCollectionView.frame=[self getPhotoFrame:_attachImageArray];
  51. [_sharePhotoCollectionView reloadData];
  52. }
  53. -(void)initUI{
  54. _sharePhotoCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[XHAlbumCollectionViewFlowLayout alloc] init]];
  55. _sharePhotoCollectionView.backgroundColor = [UIColor clearColor];
  56. [_sharePhotoCollectionView registerClass:[AlbumPhotoCollectionViewCell class] forCellWithReuseIdentifier:PhotoCollectionViewCellIdentifier];
  57. _sharePhotoCollectionView.scrollEnabled=NO;
  58. _sharePhotoCollectionView.delegate = self;
  59. _sharePhotoCollectionView.dataSource = self;
  60. [self.view addSubview:_sharePhotoCollectionView];
  61. [self.sharePhotoCollectionView reloadData];
  62. }
  63. -(NSString *)returnFormatString:(NSString *)str
  64. {
  65. return [str stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
  66. }
  67. /**
  68. 图片的frame
  69. @param photoArray <#photoArray description#>
  70. @return <#return value description#>
  71. */
  72. - (CGRect) getPhotoFrame:(NSMutableArray*)photoArray{
  73. CGRect photoCollectionViewFrame = CGRectMake(20,5, 5 * 4 + 85* 3, [self getPhotoCollectionViewHeightWithPhotos:photoArray]);
  74. return photoCollectionViewFrame;
  75. }
  76. /**
  77. 高度
  78. @param photos <#photos description#>
  79. @return <#return value description#>
  80. */
  81. - (CGFloat)getPhotoCollectionViewHeightWithPhotos:(NSArray *)photos {
  82. // 上下间隔已经在frame上做了
  83. NSInteger row = (photos.count / 3 + (photos.count % 3 ? 1 : 0));
  84. return (row *85 + (row *10));
  85. }
  86. /**
  87. 点击图片放大
  88. @param collectionView
  89. @param indexPath
  90. */
  91. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  92. [self showImageViewerAtIndexPath:indexPath];
  93. }
  94. /**
  95. 图片放大方法
  96. @param indexPath
  97. */
  98. - (void)showImageViewerAtIndexPath:(NSIndexPath *)indexPath {
  99. AlbumPhotoCollectionViewCell *cell = (AlbumPhotoCollectionViewCell *)[self.sharePhotoCollectionView cellForItemAtIndexPath:indexPath];
  100. NSMutableArray *imageViews = [NSMutableArray array];
  101. NSArray *visibleCell = [self.sharePhotoCollectionView visibleCells];
  102. NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"indexPath" ascending:YES];
  103. visibleCell = [visibleCell sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
  104. [visibleCell enumerateObjectsUsingBlock:^(AlbumPhotoCollectionViewCell *cell, NSUInteger idx, BOOL *stop) {
  105. [imageViews addObject:[[cell.contentView subviews] lastObject]];
  106. }];
  107. XHImageViewer *imageViewer = [[XHImageViewer alloc] init];
  108. [imageViewer showWithImageViews:imageViews selectedView:[[cell.contentView subviews] lastObject]];
  109. }
  110. @end