// // AttachmentViewController.m // IBOSS // // Created by 关宏厚 on 2018/11/15. // Copyright © 2018 elongtian. All rights reserved. // #import "AttachmentViewController.h" @interface AttachmentViewController () @end @implementation AttachmentViewController #pragma mark - 公共函数 - (void)viewDidLoad { [super viewDidLoad]; [self.view setBackgroundColor:[UIColor whiteColor]]; [self initUI]; } #pragma mark - 委托函数 /** 相册collectionview每个分区的项数 @param collectionView @param section @return */ - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return [_attachImageArray count]; } /** 相册加载cellectionviewcell @param collectionView @param indexPath @return */ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { AlbumPhotoCollectionViewCell *cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:PhotoCollectionViewCellIdentifier forIndexPath:indexPath]; cell1.indexPath = indexPath; ImageModel *image = _attachImageArray[indexPath.row]; NSString *imagepath = [image imageName]; imagepath= [self returnFormatString:imagepath]; if(imagepath != nil&&[imagepath length]>0){ NSString *imageUrl= [NSString stringWithFormat:@"http://%@:%@/WebService/%@",kkServerUrl,kkServerPort,imagepath]; NSURL *url= [NSURL URLWithString:[imageUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]]; [cell1.photoImageView downloadImageWithURL:url]; } return cell1; } #pragma mark - 私有函数 -(void)loadData:(NSMutableArray*)imageArray{ _attachImageArray=imageArray; _sharePhotoCollectionView.frame=[self getPhotoFrame:_attachImageArray]; [_sharePhotoCollectionView reloadData]; } -(void)initUI{ _sharePhotoCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[XHAlbumCollectionViewFlowLayout alloc] init]]; _sharePhotoCollectionView.backgroundColor = [UIColor clearColor]; [_sharePhotoCollectionView registerClass:[AlbumPhotoCollectionViewCell class] forCellWithReuseIdentifier:PhotoCollectionViewCellIdentifier]; _sharePhotoCollectionView.scrollEnabled=NO; _sharePhotoCollectionView.delegate = self; _sharePhotoCollectionView.dataSource = self; [self.view addSubview:_sharePhotoCollectionView]; [self.sharePhotoCollectionView reloadData]; } -(NSString *)returnFormatString:(NSString *)str { return [str stringByReplacingOccurrencesOfString:@"\\" withString:@"/"]; } /** 图片的frame @param photoArray <#photoArray description#> @return <#return value description#> */ - (CGRect) getPhotoFrame:(NSMutableArray*)photoArray{ CGRect photoCollectionViewFrame = CGRectMake(20,5, 5 * 4 + 85* 3, [self getPhotoCollectionViewHeightWithPhotos:photoArray]); return photoCollectionViewFrame; } /** 高度 @param photos <#photos description#> @return <#return value description#> */ - (CGFloat)getPhotoCollectionViewHeightWithPhotos:(NSArray *)photos { // 上下间隔已经在frame上做了 NSInteger row = (photos.count / 3 + (photos.count % 3 ? 1 : 0)); return (row *85 + (row *10)); } /** 点击图片放大 @param collectionView @param indexPath */ - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [self showImageViewerAtIndexPath:indexPath]; } /** 图片放大方法 @param indexPath */ - (void)showImageViewerAtIndexPath:(NSIndexPath *)indexPath { AlbumPhotoCollectionViewCell *cell = (AlbumPhotoCollectionViewCell *)[self.sharePhotoCollectionView cellForItemAtIndexPath:indexPath]; NSMutableArray *imageViews = [NSMutableArray array]; NSArray *visibleCell = [self.sharePhotoCollectionView visibleCells]; NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"indexPath" ascending:YES]; visibleCell = [visibleCell sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]]; [visibleCell enumerateObjectsUsingBlock:^(AlbumPhotoCollectionViewCell *cell, NSUInteger idx, BOOL *stop) { [imageViews addObject:[[cell.contentView subviews] lastObject]]; }]; XHImageViewer *imageViewer = [[XHImageViewer alloc] init]; [imageViewer showWithImageViews:imageViews selectedView:[[cell.contentView subviews] lastObject]]; } @end