Search code examples
objective-cuicollectionviewuicollectionviewcellios10phphotolibrary

Best Way utilize PHPhotoLibrary for display Camera Roll image in UICollectionView with custom Cell


Hello everyone I have a problem with my app ... Within my View Controller hp a CollectionView with a custom cell that should return all of the photos in the Camera Roll section of the app pictures of my iphone.

Now I've done all the steps to show the photos in a ImageView in the custom cell and up to now I have no problem ... My problem is that when I start to scroll through photos, uploading photos is very slow and immediately after the app crashes giving me back this error in the log ..

[GatekeeperXPC] Connection to assetsd was interrupted or assetsd died 25/02/2017 20: [Generic] Creating an image format with an unknown type is an error

Can you tell me if I've taken the right way to show pictures in my collection view? Where did I go wrong? because my app crashes?

Thank you all for any help you can give me

This is the code i utilize

- (void)viewDidLoad {
    [super viewDidLoad];
    self.nameTextField.delegate = self;
    self.emailTextField.delegate = self;
    self.passwordTextField.delegate = self;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor clearColor];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [_nameTextField becomeFirstResponder];
    [self queryImage];


 }

-(void)queryImage {
    PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];

    PHFetchResult *collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:fetchOptions];

    if (collection.firstObject != nil ) {
        _photoFound = YES;
        _assetCollection = collection.firstObject;

    } else {

    }

    _photoAsset = [PHAsset fetchAssetsInAssetCollection:_assetCollection options:nil];
    [_collectionView reloadData];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(80,80);
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    NSUInteger count = 0;
    if (_photoAsset != nil) {
        count = [_photoAsset count];
    }
    return count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *reuseIdentifier = @"imageCell";

    UPCameraRollCollectionViewCell* cell = [cv dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];


    PHAsset *asset = [_photoAsset objectAtIndex:indexPath.item];

PHImageManager *imageManager = [PHImageManager defaultManager];
    [imageManager requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {

[cell setThumbnailImage:result];

}];


    return cell;
}

Solution

  • Use PHCachingImageManager.

    Apple has an example that shows exactly how to do the sort of thing you're after. Collection views are precisely the intended use case.