Created
March 23, 2011 10:13
-
-
Save mr-rock/882896 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)scan { | |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | |
ALAssetsGroupType sources = ALAssetsGroupLibrary | ALAssetsGroupSavedPhotos; | |
NSMutableArray *tags = [[NSMutableArray alloc] init]; | |
ALAssetsGroupEnumerationResultsBlock assetsEnumerator = ^(ALAsset *result, NSUInteger index, BOOL *stop){ | |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | |
if (result) { | |
if ([[result valueForProperty:ALAssetPropertyType] isEqual:ALAssetTypePhoto]) { | |
NSDictionary *metadata = [[result defaultRepresentation] metadata]; | |
NSDictionary *iptc = [metadata objectForKey:(id)kCGImagePropertyIPTCDictionary]; | |
NSArray *keys = [iptc objectForKey:(id)kCGImagePropertyIPTCKeywords]; | |
NSURL *path = [[result defaultRepresentation] url]; | |
if (keys) { | |
for (id key in keys) | |
if (![tags containsObject:key]) { | |
[tags addObject:key]; | |
[keywords addObject:[[[KeywordInfoModel alloc] initWithSymbol:key andRelatedKeys:keys] autorelease]]; | |
} | |
if (![paths containsObject:path]) { | |
[paths addObject:path]; | |
[photos addObject:[[[PhotoInfoModel alloc] initWithURL:path andKeywords:keys] autorelease]]; | |
} | |
} | |
} | |
} | |
[pool release]; | |
}; | |
ALAssetsLibraryGroupsEnumerationResultsBlock groupEnumerator = ^(ALAssetsGroup *group, BOOL *stop){ | |
if (group) | |
[group enumerateAssetsUsingBlock:assetsEnumerator]; | |
else | |
[self doAfterScanning]; | |
}; | |
ALAssetsLibraryAccessFailureBlock errorBlock = ^(NSError *error){ | |
NSLog(@"Failure"); | |
}; | |
[self performSelectorOnMainThread:@selector(doBeforeScanning) withObject:nil waitUntilDone:YES]; | |
[library enumerateGroupsWithTypes:sources usingBlock:groupEnumerator failureBlock:errorBlock]; | |
[pool release]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can run [group enumerateAssetsUsingBlock:assetsEnumerator] on another thread. I think that's where you’re really blocking, because [library enumerateGroupsWithTypes:sources usingBlock:groupEnumerator failureBlock:errorBlock] is done async for me. You also seem to have an unused NSAutoreleasePool.