5.获取iOS系统中相册信息的方法

包含相关库的头文件

#import <AssetsLibrary/AssetsLibrary.h>

然后调用下面这个方法,传入搜索的类型,返回数据存在一个可变数组中

-(NSMutableArray *)searchPhotosWithTypes:(ALAssetsGroupType )types
{
    //创建一个库,对应着整个相册数据库
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];
    //创建一个可变数组,保存获取到的相册信息
    NSMutableArray *photoAssetsArray = [NSMutableArray array];
    //根据传入的类型遍历相册的所有组,对应着相册数据库中的文件夹
    [library enumerateGroupsWithTypes:types usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        if (group != nil)
        {
            //在组中遍历出所有的具体的照片或视频信息
            [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                [photoAssetsArray addObject:result];
            }];
        }
    } failureBlock:^(NSError *error) {
            NSLog(@"获取相册失败!error is ----%@",error);
    }];
    //返回获取到的照片和视频的集合
    return photoAssetsArray;
}

相关的传入类型有:

      

enum {
    ALAssetsGroupLibrary        = (1 << 0),         // The Library group that includes all assets.
    ALAssetsGroupAlbum          = (1 << 1),         // All the albums synced from iTunes or created on the device.
    ALAssetsGroupEvent          = (1 << 2),         // All the events synced from iTunes.
    ALAssetsGroupFaces          = (1 << 3),         // All the faces albums synced from iTunes.
    ALAssetsGroupSavedPhotos    = (1 << 4),         // The Saved Photos album.
#if __IPHONE_5_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
    ALAssetsGroupPhotoStream    = (1 << 5),         // The PhotoStream album.
#endif
    ALAssetsGroupAll            = 0xFFFFFFFF,       // The same as ORing together all the available group types,
};

获取到的数据是ALAsset类型,这里面包含了所有的相关信息,可以通过调用对应方法获取

获取ALAsset类型的相关属性

1.           相册封面图片 [assetsGroup  posterImage ];

2.           照片url [[[asset defaultRepresentation] url]absoluteString];

3.           照片缩略图 ?[asset thumbnail]; ?[assetaspectRatioThumbnail];

4.           照片全尺寸图 ?[[asset defaultRepresentation]fullResolutionImage];

5.          照片全屏图 ?[[asset defaultRepresentation]fullScreenImage];

6.           照片创建时间 ?[assetvalueForProperty:ALAssetPropertyDate];

7.           照片拍摄位置(可能为nil) ?[assetvalueForProperty:ALAssetPropertyLocation];

8.           照片尺寸 ?[[asset defaultRepresentation]dimensions];


注意:

1.   The lifetimes of objectsyou get back from a library instance are tied to the lifetime of the libraryinstance. ?通过ALAssetsLibrary对象获取的其他对象只在该ALAssetsLibrary对象生命期内有效,若ALAssetsLibrary对象被销毁,则其他从它获取的对象将不能被访问,否则会有错误。具体方法:ARC和非ARC区别,ARC的时候,需要将ALAssetsLibrary * libary定义为全局

MRC的时候,libary不能release

2.  ALAssetRepresentation metadata 方法很慢,我在iPhone4 iOS5.1.1中测试,此方法返回需要40-50ms,所以获取图片的个各种属性尽量直接从ALAssetRepresentation中获取,不要读取metadata 这里 给出了一个此方法占用内存过多的解释,调用多次也确实很容易会memory warning,或许也能解析其为什么很慢吧。

3.   系统相册程序显示的图片是 fullScreenImage ,而不是fullResolutionImage fullResolutionImage尺寸太大,在手机端显示推荐用fullScreenImage 

fullScreenImage已被调整过方向,可直接使用

[UIImage imageWithCGImage:representation.fullScreenImage];

 使用fullResolutionImage要自己调整方法和scale,

 [UIImage imageWithCGImage:representation.fullResolutionImage scale:representation.scale     orientation:representation.orientation];

4.遍历Assets Group

         使用 enumerateGroupsWithTypes:usingBlock:failureBlock: 方法可遍历assets group

         此方法为异步执行,若之前未被授权过,此方法会向用户请求访问数据的权限;若用户拒绝授权或其他错误则会执行failureBlock

         如果用户关掉了位置服务(Location Services,在设置->通用中),返回的错误为ALAssetsLibraryAccessGloballyDeniedError 

         enumerationBlockfailureBlock与在调用此方法的线程内执行,若要在背景线程进行遍历,可将遍历代码放入GCDNSOperation中。 

   5.遍历Assets Group中的Assets

         使用 enumerateAssetsUsingBlock: 方法或者其他遍历方法可遍历ALAssetsGroup中的所有ALAsset;

         可通过 setAssetsFilter: 设置过滤器ALssetsFilter )使enumerateAssetsUsingBlock:只返回特定类型asset,而 numberOfAssets 只返回特定类型asset的数量。 ?可以设置只显示Photo( allPhotos ),只显示Video( allVideos ),或显示全部(allAssets )

         enumerateAssetsUsingBlock:为同步方法,只有所有Asset遍历完才返回。所以需要将遍历代码放入背景线程,防止阻塞UI线程。

6.根据url获取asset

使用ALAssetsLibrary assetForURL:resultBlock:failureBlock: 方法,可根据之前从ALAssetRepresentation中获取的url重新获取ALAsset对象,此方法同enumerateGroupsWithTypes:usingBlock:failureBlock:一样为异步方法。

 


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值