• (原) 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);

    }

  • 相关阅读:
    Bootstrap4(28): 滚动监听(Scrollspy)
    Bootstrap4(27): 弹出框
    Bootstrap4(26): 提示框
    Bootstrap4(25): 模态框
    Bootstrap4(24): 轮播
    Bootstrap4(23): 自定义表单
    Bootstrap4(22): 输入框组
    Bootstrap4(21): 表单控件
    Bootstrap4(20): 表单
    Bootstrap4(19): 面包屑导航(Breadcrumb)
  • 原文地址:https://www.cnblogs.com/xiaouisme/p/2795776.html
Copyright © 2020-2023  润新知