// // ViewController.m // #import "ViewController.h" #import <Photos/Photos.h> @interface ImageCollectionViewCell: UICollectionViewCell - (void)configureWithImage:(UIImage *)image; @end @implementation ImageCollectionViewCell { UIImageView *_imageView; } - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { _imageView = [UIImageView new]; [self.contentView addSubview:_imageView]; } return self; } - (void)layoutSubviews { [super layoutSubviews]; _imageView.frame = self.contentView.bounds; } - (void)configureWithImage:(UIImage *)image { _imageView.image = image; } @end @interface ViewController () < UICollectionViewDataSource > @end @implementation ViewController { UICollectionView *_collectionView; NSMutableArray<UIImage *> *_images; } - (instancetype)init { if (self = [super initWithNibName:nil bundle:nil]) { _images = [NSMutableArray array]; } return self; } - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; _collectionView.frame = self.view.bounds; } - (void)viewDidLoad { [super viewDidLoad]; UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new]; layout.itemSize = CGSizeMake(300, 300); _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; _collectionView.dataSource = self; [_collectionView registerClass:[ImageCollectionViewCell class] forCellWithReuseIdentifier:@"Cell"]; [self.view addSubview:_collectionView]; // Do any additional setup after loading the view. [PHPhotoLibrary requestAuthorizationForAccessLevel:PHAccessLevelReadWrite handler:^(PHAuthorizationStatus status) { dispatch_async(dispatch_get_main_queue(), ^{ if (status == PHAuthorizationStatusDenied) { return; } [self fetchPhotos]; }); }]; } - (void)fetchPhotos { NSMutableArray<UIImage *> *images = [NSMutableArray array]; PHFetchOptions *const options = [PHFetchOptions new]; PHFetchResult<PHAsset *> *const fetchResult = [PHAsset fetchAssetsWithOptions:options]; [fetchResult enumerateObjectsUsingBlock:^(PHAsset * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { PHImageRequestOptions *const requestOptions = [PHImageRequestOptions new]; requestOptions.synchronous = YES; [[PHImageManager defaultManager] requestImageForAsset:obj targetSize:CGSizeMake(2000, 2000) contentMode:PHImageContentModeDefault options:requestOptions resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) { if (result) { [images addObject:result]; } }]; }]; _images = images; [_collectionView reloadData]; } #pragma mark - UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return _images.count; } - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { ImageCollectionViewCell *cell = (ImageCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; [cell configureWithImage:_images[indexPath.item]]; return cell; } @end