最佳方式利用 PHPhotoLibrary 在带有自定义 Cell 的 UICollectionView 中显示相机胶卷图像

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

大家好我的应用程序有问题...在我的视图控制器中 hp 一个带有自定义单元格的 CollectionView 应该 return 我的应用程序图片的相机胶卷部分中的所有照片iphone.

现在我已经完成了在自定义单元格的 ImageView 中显示照片的所有步骤,到目前为止我没有任何问题......我的问题是当我开始滚动浏览照片时,上传照片是非常慢,应用程序崩溃后立即在日志中返回此错误..

[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

你能告诉我在 collection 视图中显示图片的方式是否正确吗?我哪里做错了?因为我的应用程序崩溃了?

谢谢大家给我的帮助

这是我使用的代码

- (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;
}

使用PHCachingImageManager.

Apple 有一个 example 可以准确显示如何做您想要的事情。集合视图正是预期的用例。