(原) ALAssetsLibrary遍历相册及略所图
代码主要在-btn1:里面.
#import <AssetsLibrary/AssetsLibrary.h>
....
- (NSString *)makeThumbnailNameFromURL:(NSURL *)mediaURL{
if (nil == mediaURL){
return nil;
}
NSArray * components = [mediaURL.querycomponentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@"=&"]];
assert([[components objectAtIndex:0] isEqualToString:@"id"]
&& [[components objectAtIndex:2] isEqualToString:@"ext"]);
return [[components objectAtIndex:1] stringByAppendingFormat:@".%@", [components objectAtIndex:3]];
}
static NSString * strDirName4MediaThumbnail = @"MediaThumbnailDir";
- (NSURL *)saveThumbnailToCache:(CGImageRef)thumbnail name:(NSString *)imageName
{
NSFileManager *fileMgr = [NSFileManagerdefaultManager];
NSURL * cacheURL = [[fileMgr URLsForDirectory:NSCachesDirectoryinDomains:NSUserDomainMask] lastObject];
NSURL * thumbnailDir = [cacheURL URLByAppendingPathComponent:strDirName4MediaThumbnail];
if (nil == thumbnail || nil == imageName)
{
UDLog(@"error: save thumbnail failed because of thumbnail or imageName is nil");
return nil;
}
if (![fileMgr fileExistsAtPath:[thumbnailDir path]])
{
NSError * err = nil;
[fileMgr createDirectoryAtURL:thumbnailDir withIntermediateDirectories:YESattributes:nilerror:&err];
if(err)
{
UDLog(@"error: %@", err);
return nil;
}
}
NSData *image = UIImagePNGRepresentation([UIImageimageWithCGImage:thumbnail]);
NSURL * thumbnailFileURL = [thumbnailDir URLByAppendingPathComponent:imageName];
if (![image writeToURL:thumbnailFileURL atomically:YES])
{
return nil;
}
return thumbnailFileURL;
}
- (void)btn1:(id)sender
{
ALAssetsLibrary * assetsLib = [[[ALAssetsLibraryalloc] init] autorelease];
[assetsLib enumerateGroupsWithTypes:ALAssetsGroupAllusingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if(group == nil)
return;
//
NSString * groupName = [group valueForProperty:ALAssetsGroupPropertyName];
UDLog(@"\n------------\ngroupName: %@\n\n", groupName);
#if UDebug
if([group numberOfAssets] > 100)
{
UDLog(@"group has %d assets, to many so I return when debug test.", [group numberOfAssets]);
return;
}
#endif
//
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
//NSString* assetType = [result valueForProperty:ALAssetPropertyType];
//if ([assetType isEqualToString:ALAssetTypePhoto])
if(result)
{
UDLog(@"-------------");
ALAssetRepresentation *pro = [result defaultRepresentation];
NSURL * mediaURL = pro.url;
NSURL * thumbnailFileURL = [self saveThumbnailToCache:result.thumbnail
name:[self makeThumbnailNameFromURL:mediaURL]];
UDLog(@"mediaURL:\n%@", mediaURL);
UDLog(@"thumbnailFileURL:\n%@", thumbnailFileURL);
#if UDebug
*stop = YES;
//
NSDictionary * dict = pro.metadata;
UDBreak;
#endif
}
}];
} failureBlock:^(NSError *error) {
UDLog(@"error :%@", error);
}];
}
- (void)btn2:(id)sender
{
NSFileManager *fm = [NSFileManagerdefaultManager];
NSURL * cacheURL = [[fm URLsForDirectory:NSCachesDirectoryinDomains:NSUserDomainMask] lastObject];
NSURL * thumbnailDir = [cacheURL URLByAppendingPathComponent:strDirName4MediaThumbnail];
BOOL canDel = [fm isDeletableFileAtPath:[thumbnailDir path]];
assert(canDel);
NSError * err = nil;
[fm removeItemAtURL:thumbnailDir error:&err];
if(err)
UDLog(@"err:%@", err);
}