使用ALAssetsLibrary读取所有照片
2016-12-29 10:32
483 查看
一.ALAssetsLibrary介绍
ALAssetsLibrary提供了访问iOS设备下”照片”应用下所有照片和视频的接口;
从ALAssetsLibrary中可读取所有的相册数据,即ALAssetsGroup对象列表;
从每个ALAssetsGroup中可获取到其中包含的照片或视频列表,即ALAsset对象列表;
每个ALAsset可能有多个representations表示,即ALAssetRepresentation对象,使用其defaultRepresentation方法可获得其默认representations,使用[asset valueForProperty:ALAssetPropertyRepresentations]可获取其所有representations的UTI数组。
从ALAsset对象可获取缩略图thumbnail或aspectRatioThumbnail;
从ALAssetRepresentation对象可获取全尺寸图片(fullResolutionImage),全屏图片(fullScreenImage)及图片的各种属性:orientation,dimensions,scale,url,metadata等。
其层次关系为ALAssetsLibrary -> ALAssetsGroup -> ALAsset -> ALAssetRepresentation。
注意:
The lifetimes of objects you get back from a library instance are tied to the lifetime of the library instance.
通过ALAssetsLibrary对象获取的其他对象只在该ALAssetsLibrary对象生命期内有效,若ALAssetsLibrary对象被销毁,则其他从它获取的对象将不能被访问,否则有会错误。
invalid attempt to access ALAssetPrivate past the lifetime of its owning ALAssetsLibrary
ALAssetRepresentation的metadata方法很慢,我在iPhone4 iOS5.1.1中测试,此方法返回需要40-50ms,所以获取图片的个各种属性尽量直接从ALAssetRepresentation中获取,不要读取metadata。
这里给出了一个此方法占用内存过多的解释,调用多次也确实很容易会memory warning,或许也能解析其为什么很慢吧。
The method [representation metadata] returns an autoreleased object and possibly creates more autoreleased objects when it executes. All these instances are added to the autorelease pool,
waiting to be finally released (and their memory freed) when the ARP gets the chance to drain itself.
系统”相册”程序显示的图片是fullScreenImage,而不是fullResolutionImage,fullResolutionImage尺寸太大,在手机端显示推荐用fullScreenImage。
fullScreenImage已被调整过方向,可直接使用,即
[UIImage imageWithCGImage:representation.fullScreenImage];
使用fullResolutionImage要自己调整方法和scale,即
[UIImage imageWithCGImage:representation.fullResolutionImage scale:representation.scale orientation:representation.orientation];
二.创建 ALAssetsLibrary对象
使用ALAssetsLibrary之前需导入头文件和AssetsLibrary.framework。
#import
...
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc]init];
...
三.遍历Assets Group
使用enumerateGroupsWithTypes:usingBlock:failureBlock:方法可遍历assets group;
此方法为异步执行,若之前未被授权过,此方法会向用户请求访问数据的权限;若用户拒绝授权或其他错误则会执行failureBlock;
如果用户关掉了位置服务(Location Services,在设置->通用中),返回的错误为ALAssetsLibraryAccessGloballyDeniedError。
enumerationBlock和failureBlock与在调用此方法的线程内执行,若要在背景线程进行遍历,可将遍历代码放入GCD或NSOperation中。
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (!group) {
[tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}else{
[groupsArray addObject:group];
...
}
} failureBlock:^(NSError *error) {
NSLog(@"error:%@",error);
}];
四.遍历Assets Group中的Assets
使用enumerateAssetsUsingBlock:方法或者其他变体方法可遍历ALAssetsGroup中的所有ALAsset;
可通过setAssetsFilter:设置过滤器(ALssetsFilter)使enumerateAssetsUsingBlock:只返回特定类型asset,而numberOfAssets只返回特定类型asset的数量。
可以设置只显示Photo(allPhotos),只显示Video(allVideos),或显示全部(allAssets)。
enumerateAssetsUsingBlock:为同步方法,只有所有Asset遍历完才返回。所以需要将遍历代码放入背景线程,防止阻塞UI线程。
[assetsGroup setAssetsFilter:[ALAssetsFilter allPhotos]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[assetsGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (!result) {
[tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}else{
[assetsArray addObject:result];
...
}
}];
});
五.根据url获取asset
使用ALAssetsLibrary的assetForURL:resultBlock:failureBlock:方法,可根据之前从ALAssetRepresentation中获取的url重新获取ALAsset对象,此方法同enumerateGroupsWithTypes:usingBlock:failureBlock:一样为异步方法。
六.获取Assets的各种属性
相册封面图片
[assetsGroup posterImage];
照片url
[[[asset defaultRepresentation] url] absoluteString];
照片缩略图
[asset thumbnail];
[asset aspectRatioThumbnail];
照片全尺寸图
[[asset defaultRepresentation] fullResolutionImage];
照片全屏图
[[asset defaultRepresentation] fullScreenImage];
照片创建时间
[asset valueForProperty:ALAssetPropertyDate];
照片拍摄位置(可能为nil)
[asset valueForProperty:ALAssetPropertyLocation];
照片尺寸
[[asset defaultRepresentation] dimensions];
ALAssetsLibrary提供了访问iOS设备下”照片”应用下所有照片和视频的接口;
从ALAssetsLibrary中可读取所有的相册数据,即ALAssetsGroup对象列表;
从每个ALAssetsGroup中可获取到其中包含的照片或视频列表,即ALAsset对象列表;
每个ALAsset可能有多个representations表示,即ALAssetRepresentation对象,使用其defaultRepresentation方法可获得其默认representations,使用[asset valueForProperty:ALAssetPropertyRepresentations]可获取其所有representations的UTI数组。
从ALAsset对象可获取缩略图thumbnail或aspectRatioThumbnail;
从ALAssetRepresentation对象可获取全尺寸图片(fullResolutionImage),全屏图片(fullScreenImage)及图片的各种属性:orientation,dimensions,scale,url,metadata等。
其层次关系为ALAssetsLibrary -> ALAssetsGroup -> ALAsset -> ALAssetRepresentation。
注意:
The lifetimes of objects you get back from a library instance are tied to the lifetime of the library instance.
通过ALAssetsLibrary对象获取的其他对象只在该ALAssetsLibrary对象生命期内有效,若ALAssetsLibrary对象被销毁,则其他从它获取的对象将不能被访问,否则有会错误。
invalid attempt to access ALAssetPrivate past the lifetime of its owning ALAssetsLibrary
ALAssetRepresentation的metadata方法很慢,我在iPhone4 iOS5.1.1中测试,此方法返回需要40-50ms,所以获取图片的个各种属性尽量直接从ALAssetRepresentation中获取,不要读取metadata。
这里给出了一个此方法占用内存过多的解释,调用多次也确实很容易会memory warning,或许也能解析其为什么很慢吧。
The method [representation metadata] returns an autoreleased object and possibly creates more autoreleased objects when it executes. All these instances are added to the autorelease pool,
waiting to be finally released (and their memory freed) when the ARP gets the chance to drain itself.
系统”相册”程序显示的图片是fullScreenImage,而不是fullResolutionImage,fullResolutionImage尺寸太大,在手机端显示推荐用fullScreenImage。
fullScreenImage已被调整过方向,可直接使用,即
[UIImage imageWithCGImage:representation.fullScreenImage];
使用fullResolutionImage要自己调整方法和scale,即
[UIImage imageWithCGImage:representation.fullResolutionImage scale:representation.scale orientation:representation.orientation];
二.创建 ALAssetsLibrary对象
使用ALAssetsLibrary之前需导入头文件和AssetsLibrary.framework。
#import
...
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc]init];
...
三.遍历Assets Group
使用enumerateGroupsWithTypes:usingBlock:failureBlock:方法可遍历assets group;
此方法为异步执行,若之前未被授权过,此方法会向用户请求访问数据的权限;若用户拒绝授权或其他错误则会执行failureBlock;
如果用户关掉了位置服务(Location Services,在设置->通用中),返回的错误为ALAssetsLibraryAccessGloballyDeniedError。
enumerationBlock和failureBlock与在调用此方法的线程内执行,若要在背景线程进行遍历,可将遍历代码放入GCD或NSOperation中。
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (!group) {
[tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}else{
[groupsArray addObject:group];
...
}
} failureBlock:^(NSError *error) {
NSLog(@"error:%@",error);
}];
四.遍历Assets Group中的Assets
使用enumerateAssetsUsingBlock:方法或者其他变体方法可遍历ALAssetsGroup中的所有ALAsset;
可通过setAssetsFilter:设置过滤器(ALssetsFilter)使enumerateAssetsUsingBlock:只返回特定类型asset,而numberOfAssets只返回特定类型asset的数量。
可以设置只显示Photo(allPhotos),只显示Video(allVideos),或显示全部(allAssets)。
enumerateAssetsUsingBlock:为同步方法,只有所有Asset遍历完才返回。所以需要将遍历代码放入背景线程,防止阻塞UI线程。
[assetsGroup setAssetsFilter:[ALAssetsFilter allPhotos]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[assetsGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (!result) {
[tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}else{
[assetsArray addObject:result];
...
}
}];
});
五.根据url获取asset
使用ALAssetsLibrary的assetForURL:resultBlock:failureBlock:方法,可根据之前从ALAssetRepresentation中获取的url重新获取ALAsset对象,此方法同enumerateGroupsWithTypes:usingBlock:failureBlock:一样为异步方法。
六.获取Assets的各种属性
相册封面图片
[assetsGroup posterImage];
照片url
[[[asset defaultRepresentation] url] absoluteString];
照片缩略图
[asset thumbnail];
[asset aspectRatioThumbnail];
照片全尺寸图
[[asset defaultRepresentation] fullResolutionImage];
照片全屏图
[[asset defaultRepresentation] fullScreenImage];
照片创建时间
[asset valueForProperty:ALAssetPropertyDate];
照片拍摄位置(可能为nil)
[asset valueForProperty:ALAssetPropertyLocation];
照片尺寸
[[asset defaultRepresentation] dimensions];
相关文章推荐
- 使用ALAssetsLibrary读取所有照片
- 使用ALAssetsLibrary读取所有照片
- 使用ALAssetsLibrary读取所有照片
- 使用ALAssetsLibrary读取所有照片
- 使用ALAssetsLibrary读取所有照片
- 使用ALAssetsLibrary读取所有照片
- 使用ALAssetsLibrary读取所有照片
- 使用ALAssetsLibrary读取所有照片
- 使用ALAssetsLibrary读取所有照片
- 使用ALAssetsLibrary读取所有照片
- ALAssetsLibrary 读取所有照片
- iOS --- 使用ALAssetsLibrary访问设备中的所有照片信息
- ALAssetsLibrary读取所有照片
- 读取照片的Exif信息,这篇文章则是使用了ImageIO类来获取照片的信息
- 使用OleDb读取Excel所有的Table并用Sheet名称作为表名称
- 使用VC实现读取指定路径下的文件,并显示所有文件的文件名和文件图标
- xmemcached使用,并读取memcached服务器上所有的缓存数据
- 教你如何在TFS中让所有的成员都能够通过使用Project来读取计划的Start Date 时间
- 使用jQuery匹配文档中所有的li元素,返回一个jQuery对象,然后通过数组下标的方式读取jQuery集合中第1个DOM元素,此时返回的是DOM对象,然后调用DOM属性innerHTML,读取该元素 包含的文本信息
- C#不使用DataSet操作XML,XmlDocument读写xml所有节点及读取xml节点的数据总结