2017년 1월 8일 일요일

겔러리에서 모든 이미지 불러오기

-(void)getAllPhotos
{
    [[Util sharedInstance] showLoadingView];
    
    NSMutableArray *photos = [[NSMutableArray alloc] init];
    PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
    allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
    PHFetchResult *allPhotos = [PHAsset fetchAssetsWithOptions:allPhotosOptions];
    PHFetchResult *fetchResult = @[allPhotos][0];
    
    for (int x = 0; x < fetchResult.count; x ++) {
        
        PHAsset *asset = fetchResult[x];
        photos[x] = asset;
    }
    
    self.assets = photos;
    
    [[Util sharedInstance] hideLoadingView];
    [_collectionView reloadData];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.selectedAssets = [[NSMutableArray alloc] init];
    self.assets = [[NSMutableArray alloc] init];
    
    self.manager = [PHImageManager defaultManager];
    self.options = [[PHImageRequestOptions alloc] init];
    _options.resizeMode = PHImageRequestOptionsResizeModeFast;
    _options.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat;
    
    [self requestAuthorizationWithRedirectionToSettings];
    
    //[self performSelector:@selector(getAllPhotos) withObject:nil];

}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    BoardPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BoardPhotoCell" forIndexPath:indexPath];
    
    cell.checkIndicator.hidden = NO;
    PHAsset *asset = [_assets objectAtIndex:indexPath.item];
    [_manager requestImageForAsset:asset
                       targetSize:CGSizeMake(1024.f,1024.f)
                      contentMode:PHImageContentModeDefault
                          options:_options
                    resultHandler:^void(UIImage *image, NSDictionary *info) {
                        cell.photo.image = image;
                    }];
    
    //update checkbox
    NSUInteger foundIndex = [_selectedAssets indexOfObject:asset];
    if (foundIndex != NSNotFound) {
        [cell setCheck:YES];
    } else {
        [cell setCheck:NO];
    }
    
    return cell;

}

댓글 없음:

댓글 쓰기