JFImagePickerController、多选图片
JFImagePickerController、多选图片
iOS赵师顺 发表于1年前
JFImagePickerController、多选图片
  • 发表于 1年前
  • 阅读 10
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 学生专属云服务套餐 10元起购>>>   

摘要: 通过使用JFImagePickerController第三方,我们只需要初始化一个数组接受图片,一个createCollectionView显示图片,实现多选图片,通常用来做朋友圈,类似QQ发表动态选图。

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];

}

共有 人打赏支持
粉丝 4
博文 70
码字总数 33551
×
iOS赵师顺
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: