Skip to content

Commit

Permalink
Use |stop| to stop the enumeration of ALAssetsLibraryGroupsEnumeratio…
Browse files Browse the repository at this point in the history
…nResultsBlock #10
  • Loading branch information
Kjuly committed Sep 22, 2013
1 parent e050db0 commit 5d267f5
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions ALAssetsLibrary-CustomPhotoAlbum/ALAssetsLibrary+CustomPhotoAlbum.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ - (ALAssetsLibraryWriteImageCompletionBlock)_resultBlockOfAddingToAlbum:(NSStrin
failure:(ALAssetsLibraryAccessFailureBlock)failure
{
return ^(NSURL *assetURL, NSError *error) {
// run the completion block for writing image to saved
// Run the completion block for writing image to saved
// photos album
//if (completion) completion(assetURL, error);

// if an error occured, do not try to add the asset to
// If an error occured, do not try to add the asset to
// the custom photo album
if (error != nil) {
if (failure) failure(error);
return;
}

// add the asset to the custom photo album
// Add the asset to the custom photo album
[self addAssetURL:assetURL
toAlbum:albumName
completion:completion
Expand All @@ -76,9 +76,9 @@ - (ALAssetsLibraryAssetForURLResultBlock)_assetForURLResultBlockWithGroup:(ALAss
failure:(ALAssetsLibraryAccessFailureBlock)failure
{
return ^(ALAsset *asset) {
// add photo to the target album
// Add photo to the target album
if ([group addAsset:asset]) {
// run the completion block if the asset was added successfully
// Run the completion block if the asset was added successfully
if (completion) completion(assetURL, nil);
}
// |-addAsset:| may fail (return NO) if the group is not editable,
Expand Down Expand Up @@ -137,11 +137,17 @@ - (void)addAssetURL:(NSURL *)assetURL
{
__block BOOL albumWasFound = NO;

// Signature for the block executed when a match is found during enumeration using
// |-enumerateGroupsWithTypes:usingBlock:failureBlock:|.
//
// |group|: The current asset group in the enumeration.
// |stop| : A pointer to a boolean value; set the value to YES to stop enumeration.
//
ALAssetsLibraryGroupsEnumerationResultsBlock enumerationBlock;
enumerationBlock = ^(ALAssetsGroup *group, BOOL *stop) {
// compare the names of the albums
// Compare the names of the albums
if ([albumName compare:[group valueForProperty:ALAssetsGroupPropertyName]] == NSOrderedSame) {
// target album is found
// Target album is found
albumWasFound = YES;

// Get a hold of the photo's asset instance
Expand All @@ -156,29 +162,29 @@ - (void)addAssetURL:(NSURL *)assetURL
resultBlock:assetForURLResultBlock
failureBlock:failure];

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

if (group == nil && albumWasFound == NO) {
// photo albums are over, target album does not exist, thus create it
// Photo albums are over, target album does not exist, thus create it

// Since you use the assets library inside the block,
// ARC will complain on compile time that there’s a retain cycle.
// When you have this – you just make a weak copy of your object.
__weak ALAssetsLibrary * weakSelf = self;

// if iOS version is lower than 5.0, throw a warning message
// If iOS version is lower than 5.0, throw a warning message
if (! [self respondsToSelector:@selector(addAssetsGroupAlbumWithName:resultBlock:failureBlock:)])
NSLog(@"![WARNING][LIB:ALAssetsLibrary+CustomPhotoAlbum]: \
|-addAssetsGroupAlbumWithName:resultBlock:failureBlock:| \
only available on iOS 5.0 or later. \
ASSET cannot be saved to album!");
// create new assets album
// Create new assets album
else {
[self addAssetsGroupAlbumWithName:albumName
resultBlock:^(ALAssetsGroup *createdGroup) {
// get the photo's instance
// Get the photo's instance
// add the photo to the newly created album
ALAssetsLibraryAssetForURLResultBlock assetForURLResultBlock =
[weakSelf _assetForURLResultBlockWithGroup:createdGroup
Expand All @@ -191,13 +197,12 @@ - (void)addAssetURL:(NSURL *)assetURL
}
failureBlock:failure];
}

// should be the last iteration anyway, but just in case
return;
// Should be the last iteration anyway, but just in case
*stop = YES;
}
};

// search all photo albums in the library
// Search all photo albums in the library
[self enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:enumerationBlock
failureBlock:failure];
Expand Down

0 comments on commit 5d267f5

Please sign in to comment.