Skip to content

Commit

Permalink
Add |-loadAssetsForProperty:fromAlbum:completion:|
Browse files Browse the repository at this point in the history
  • Loading branch information
Kjuly committed Aug 30, 2015
1 parent f943321 commit 8c12cd2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@
completion:(ALAssetsLibraryWriteImageCompletionBlock)completion
failure:(ALAssetsLibraryAccessFailureBlock)failure;

/*! Loads assets w/ desired property from the assets group (album)
*
* \param property Property for the asset, refer to ALAsset.h, if not offered, just return instances of ALAsset
* \param albumName Custom album name
* \param completion Block to be executed when succeed or failed to load assets from target album
*/
- (void)loadAssetsForProperty:(NSString *)property
fromAlbum:(NSString *)albumName
completion:(void (^)(NSMutableArray *array, NSError *error))completion;

/*! Loads assets from the assets group (album)
*
* \param albumName Custom album name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,45 @@ - (void)addAssetURL:(NSURL *)assetURL
failureBlock:failure];
}

- (void)loadAssetsForProperty:(NSString *)property
fromAlbum:(NSString *)albumName
completion:(void (^)(NSMutableArray *, NSError *))completion
{
ALAssetsLibraryGroupsEnumerationResultsBlock block = ^(ALAssetsGroup *group, BOOL *stop) {
// Checking if library exists
if (group == nil) {
*stop = YES;
return;
}

// If we have found library with given title we enumerate it
if ([albumName compare:[group valueForProperty:ALAssetsGroupPropertyName]] == NSOrderedSame) {
NSMutableArray * array = [[NSMutableArray alloc] init];
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
// Checking if group isn't empty
if (! result) return;

[array addObject:(property ? ([result valueForProperty:property] ?: [NSNull null]) : result)];
}];

// Execute the |completion| block
if (completion) completion(array, nil);

// Album was found, bail out of the method
*stop = YES;
}
};

ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *error) {
NSLog(@"%s: %@", __PRETTY_FUNCTION__, [error localizedDescription]);
if (completion) completion(nil, error);
};

[self enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:block
failureBlock:failureBlock];
}

- (void)loadImagesFromAlbum:(NSString *)albumName
completion:(void (^)(NSMutableArray *, NSError *))completion
{
Expand Down

0 comments on commit 8c12cd2

Please sign in to comment.