iOS - 獲取手機中所有圖片


  1 #import <AssetsLibrary/AssetsLibrary.h>
  3 
  4 
  5 /**
  6  *  ALAssetsLibrary.h 代表資源庫(所有的視頻,照片)
  7     ALAssetsGroup.h   代表資源庫中的相冊
  8     ALAsset.h         代表相冊中一個視頻或者一張照片
  9     ALAssetRepresentation.h 代表一個資源的描述,可以獲取到原始圖片
 10  */
 11 
 12 @interface ViewController ()
 13 
 14 @property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
 15 
 16 @property (nonatomic,strong)NSMutableArray *array;
 17 @end
 18 
 19 @implementation ViewController{
 20 
 21     ALAssetsLibrary *library;
 22 
 23 }
 24 
 25 - (void)viewDidLoad {
 26     [super viewDidLoad];
 27     
 28     //創建可變數組,存儲資源文件
 29     _array = [NSMutableArray array];
 30     
 31     //創建資源庫,用於訪問相冊資源
 32     library = [[ALAssetsLibrary alloc] init];
 33     
 34     //遍歷資源庫中所有的相冊,有多少個相冊,usingBlock會調用多少次
 35     [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
 36         
 37         //如果存在相冊,再遍歷
 38         if (group) {
 39             
 40             //遍歷相冊中所有的資源(照片,視頻)
 41            [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
 42                
 43                /*
 44                 資源的索引
 45                if (index == 2) {
 46                    
 47                 //停止遍歷
 48                    *stop = YES;
 49                }
 50                 */
 51                
 52                if (result) {
 53                    //將資源存儲到數組中
 54                    [_array addObject:result];
 55                }
 56                
 57            }];
 58         }
 59         
 60         //刷新_collectionView reloadData;
 61          [_collectionView reloadData];
 62         
 63     } failureBlock:^(NSError *error) {
 64         
 65         NSLog(@"訪問失敗");
 66     }];
 67     
 68 }
 69 
 70 #pragma mark -UICollectionViewDelegate
 71 
 72 //行的個數
 73 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
 74 
 75 
 76     return _array.count;
 77 
 78 }
 79 
 80 //創建UICollectionViewCell
 81 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
 82     
 83     //如果單元格是在故事版中畫出來的,不需要注冊,需要在單元格中指定標識符
 84     UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
 85     
 86     //取得圖片視圖
 87     UIImageView *imgView = (UIImageView *)[cell.contentView viewWithTag:1];
 88     
 89     //取出對應的資源數據
 90      ALAsset *result =_array[indexPath.row];
 91     
 92     //獲取到縮略圖
 93     CGImageRef cimg = [result thumbnail];
 94     
 95     //轉換為UIImage
 96     UIImage *img = [UIImage imageWithCGImage:cimg];
 97 
 98     //顯示圖片
 99     imgView.image = img;
100     
101     /**
102      *  獲取到原始圖片
103      ALAssetRepresentation *presentation = [result defaultRepresentation];
104     
105     CGImageRef resolutionImg = [presentation fullResolutionImage];
106      */
107 
108     return cell;
109 
110 }
111 
112 //單元格大小
113 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
114 
115     return CGSizeMake(70, 70);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM