How to get images from Custom album of Photos, iOS SDK?

怎甘沉沦 提交于 2019-12-03 14:55:38

Here is the code that works.

__block PHAssetCollection *collection;

// Find the album
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", @"YOUR_CUSTOM_ALBUM_NAME"];
collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
                                                  subtype:PHAssetCollectionSubtypeAny
                                                  options:fetchOptions].firstObject;

PHFetchResult *collectionResult = [PHAsset fetchAssetsInAssetCollection:collection options:nil];

[collectionResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {


    //add assets to an array for later use in the uicollectionviewcell

}];

Just try once this code.. You can get all images in one array using this code:-

-(void)getAllPictures
{

    __block PHAssetCollection *collection;

    // Find the album
    PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
    fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", @"YOUR_CUSTOM_ALBUM_NAME"];
    collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
                                                          subtype:PHAssetCollectionSubtypeAny
                                                          options:fetchOptions].firstObject;

    PHFetchResult *collectionResult = [PHAsset fetchAssetsInAssetCollection:collection options:nil];

    NSMutableArray *assets = [[NSMutableArray alloc] init];

    [collectionResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
        [assets addObject:asset];

    }];

    PHImageRequestOptions * requestOptions = [[PHImageRequestOptions alloc] init];
    //requestOptions.resizeMode   = PHImageRequestOptionsResizeModeExact;
    //requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;

    // this one is key
    //requestOptions.synchronous = true;


    PHImageManager *manager = [PHImageManager defaultManager];
    NSMutableArray *images = [NSMutableArray arrayWithCapacity:[assets count]];

    // assets contains PHAsset objects.
    __block UIImage *ima;

    for (PHAsset *asset in assets) {
        // Do something with the asset

        [manager requestImageForAsset:asset
                           targetSize:PHImageManagerMaximumSize//CGSizeMake(300, 300)
                          contentMode:PHImageContentModeDefault
                              options:requestOptions
                        resultHandler:^void(UIImage *image, NSDictionary *info) {
                            ima = image;

                            [images addObject:ima];
                        }];
    }

    NSLog(@"images %@", images); //You will get all images into this images array.

}

For more details, Please refer Example app using Photos framework.

Hope, this is what you're looking for. Any concern get back to me. :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!