美文网首页
选中系统照片库中多张照片的方法

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

作者: 狒狒James_Leo | 来源:发表于2016-09-01 11:23 被阅读0次
    //
    //  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
    
    

    相关文章

      网友评论

          本文标题:选中系统照片库中多张照片的方法

          本文链接:https://www.haomeiwen.com/subject/nkbcettx.html