1、导入框架 遵守协议,初始化数组,UICollectionView; #import "ViewController.h" #import "JFImagePickerController.h"
1、导入框架 遵守协议,初始化数组,UICollectionView;
#import "ViewController.h"
#import "JFImagePickerController.h"
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,JFImagePickerDelegate>
@property (nonatomic,strong)NSMutableArray *photosArray;
@property (nonatomic,strong)UICollectionView *photosList;
@end
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"多选图片";
self.extendedLayoutIncludesOpaqueBars = YES;
self.automaticallyAdjustsScrollViewInsets = NO;
self.edgesForExtendedLayout = UIRectEdgeAll;
_photosArray = [[NSMutableArray alloc]init];
[self createAddPhotosButton];
[self createCollectionView];
}
- (void)createAddPhotosButton{
UIBarButtonItem *addPhotos = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addButton:)];
self.navigationItem.rightBarButtonItem = addPhotos;
}
// 点击添加按钮,进入JFImagePickerController,选图片
- (void)addButton:(UIBarButtonItem *)sender{
JFImagePickerController *picker = [[JFImagePickerController alloc] initWithRootViewController:nil];
picker.pickerDelegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
- (void)createCollectionView{
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.minimumInteritemSpacing = 0;
flowLayout.minimumLineSpacing = 3;
NSInteger size = [UIScreen mainScreen].bounds.size.width/4-1;
if (size%2!=0) {
size-=1;
}
flowLayout.itemSize = CGSizeMake(size, size);
flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
_photosList = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
_photosList.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
_photosList.scrollIndicatorInsets = _photosList.contentInset;
_photosList.delegate = self;
_photosList.dataSource = self;
_photosList.backgroundColor = [UIColor whiteColor];
_photosList.alwaysBounceVertical = YES;
[self.view addSubview:_photosList];
[_photosList registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"imagePickerCell"];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return _photosArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"imagePickerCell" forIndexPath:indexPath];
ALAsset *asset = _photosArray[indexPath.row];
UIImageView *imgView = (UIImageView *)[cell.contentView viewWithTag:1];
if (!imgView) {
imgView = [[UIImageView alloc] initWithFrame:cell.bounds];
imgView.contentMode = UIViewContentModeScaleAspectFill;
imgView.clipsToBounds = YES;
imgView.tag = 1;
[cell addSubview:imgView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(preview:)];
[cell addGestureRecognizer:tap];
}
cell.tag = indexPath.item;
[[JFImageManager sharedManager] thumbWithAsset:asset resultHandler:^(UIImage *result) {
if (cell.tag==indexPath.item) {
imgView.image = result;
}
}];
return cell;
}
- (void)preview:(UITapGestureRecognizer *)tap{
UIView *temp = tap.view;
JFImagePickerController *picker = [[JFImagePickerController alloc] initWithPreviewIndex:temp.tag];
picker.pickerDelegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
#pragma mark - delegate
// 选择结束后
- (void)imagePickerDidFinished:(JFImagePickerController *)picker{
[_photosArray removeAllObjects];
[_photosArray addObjectsFromArray:picker.assets];
[_photosList reloadData];
[self imagePickerDidCancel:picker];}
// 取消选择
- (void)imagePickerDidCancel:(JFImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
}