选中系统照片库中多张照片的方法

//
//  ViewController.m
//  SelectMorePhoto
//
//  Created by phc on 16/7/17.
//  Copyright © 2016年 phc. All rights reserved.
//

#import "ViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)NSMutableArray *imgArr;
@property (strong, nonatomic) UICollectionView *collectionView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    _imgArr = [NSMutableArray array];
    //1.创建资源库对象(资源库中包含所有的视频和照片)
      ALAssetsLibrary * library = [[ALAssetsLibrary alloc] init];
     //遍历资源库
      [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
          //ALAssetsGroup代表资源库中的一个相册
          if (group) {//如果存在,在遍历
               //遍历相册中的所有的资源(包括照片,和视频)
              [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                  /*
                  if (index == 3) {
                      
                      *stop = YES;
                  }
                  */
                 //ALAsset代表一个照片
                //获取图片的缩略图
                 CGImageRef cimg = [result thumbnail];
                 UIImage *img = [UIImage imageWithCGImage:cimg];
                  //容错
                  if (img) {
                      
                      [_imgArr addObject:img];
                  }
              }];
          }
      [_collectionView reloadData];
          
      } failureBlock:^(NSError *error) {
          
          NSLog(@"访问失败");
      }];
    
    [self creactCollection];
    
}

- (void)creactCollection{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    //设置单元格尺寸
    flowLayout.itemSize = CGSizeMake(70, 70);
    _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor greenColor];
    //设置代理方法和数据源方法
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [self.view addSubview:_collectionView];
    
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _imgArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.bounds];
    imgView.backgroundColor = [UIColor redColor];
    [cell.contentView addSubview:imgView];
    imgView.image = _imgArr[indexPath.row];
    
    return cell;
       
}


@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • (一)全面性 通过就业质量的定义可以看出,就业质量是一个多维度的概念,它包括个人在就业过程的每一个环节上被公平对待...
    蔡蔡Tacy阅读 366评论 0 0
  • 城阙辅三秦,风烟望五津。 与君离别意,同是宦游人。 海内存知己,天涯若比邻。 无为在歧路,儿女共沾巾。 初唐四杰:...
    5562d88ec370阅读 282评论 0 1
  • 2016.2.2 终于等到澳洲移民局确认信,签证申请下来了。那天刚好是site 在酒店开未来展望会,我一开始控制激...
    CiCi野良阅读 192评论 0 0
  • 洛淑萍(品味休闲6) 喜欢, 一个人独坐在窗前。 倾听着, 内心的呼唤, 任由思绪放荡无边…… 喜欢, 一个人静坐...
    品味休闲阅读 313评论 0 3