使用ALAssetsLibrary读取所有照片
一. 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];
转载自:http://www.tuicool.com/articles/UBZJFb
使用ALAssetsLibrary读取所有照片的更多相关文章
- iOS---如何获取手机的本地照片和相册
__weak ViewController *weakSelf = self; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIO ...
- 如何读取Access里的OLE类型的图片
身份证一类读卡器读取的照片信息,保存在Access数据库中一般为OLE型字段,图片为BMP格式,因为是用其读卡器写入的,其数据类型为常二进制数据. 再用报表或EXCEL读取这些图片时,如果将该图片字段 ...
- iOS开发之保存照片到自己创建的相簿
iOS开发之保存照片到自己创建的相簿 保存照片还可以用ALAssetsLibrary,ALAssetsLibrary提供了我们对iOS设备中的相片.视频的访问,是连接应用程序和相册之间访问的一个桥梁. ...
- Java读取本地文件,并显示在JSP文件中
当我们初学IMG标签时,我们知道通过设置img标签的src属性,能够在页面中显示想要展示的图片.其中src的值,可以是磁盘目录上的绝对,也可以是项目下的相对路径,还可以是网络上的图片路径.在存 ...
- qt之数据库对照片的存取
需要确保数据库连接上 QOCI为驱动 //oracle 数据库连接 //需要在执行文件目录添加 oci.dll oraociei11.dll QSqlDatabase db = QSqlDatabas ...
- [Xcode 实际操作]九、实用进阶-(12)从系统相册中读取图片
目录:[Swift]Xcode实际操作 本文将演示从系统相册中读取图片. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //添加两个协议 ...
- S1的小成果:MyKTV系统
转眼之间,已经到了2016年,即新的一年了!S1也结束了,收获的也不多 ,想想最后留给大家的就一个KTV项目了. 希望大家看时有所收获 现在我们一起来看KTV前台管理 主界面的运行 ...
- 【转】Windows Phone在隔离存储里存取图片文件
一共两个页面,第一个为MainPage.xaml,代码如下: <!--ContentPanel - place additional content here--> <Grid x: ...
- 利用canvas实现抽奖转盘---转载别人的
功能需求 转盘要美观,转动效果流畅. 转盘上需要显示奖品图片,并且奖品是后台读取的照片和名字. 转动动画完成后要有相应提示. 获取的奖品具体算法在数据库里操作,前端只提供最后的效果展示. 知识要点 ...
随机推荐
- mogilefsdBUG mogilefsd[15624]: crash log: Modification of a read-only value attempted at /usr/local/share/perl5/Sys/Syscall.pm line 227
mogilefsd[15624]: crash log: Modification of a read-only value attempted at /usr/local/share/perl5/S ...
- xpath测试工具 xpath调试工具
其实这个工具没什么介绍的了 因为目前有项目中需要到了xpath语法,然后呢,有没有什么好的工具 大部分都是联网的,个人比较喜欢离线的工具包 所以顺手就写了一个小工具来测试xpath语法的正确性 so, ...
- 如何跳转到其他APP(android)
有很多小伙伴会遇上这样的需求,从自己的app页面跳转到其他APP界面,一般情况下都是在自己的主包中跳转到公司其他APP,或者是合作方的APP,如果手机中没有这款APP会下载这款APP . 今天,博主就 ...
- Dell Inspiron 7520 安装Ubuntu 14.04 LTS
我的电脑是Dell Inspiron 7520,之前用的Windows 7, 装了虚拟机,再在虚拟机里面装Ubuntu, 电脑一直卡顿,一怒之下,升级了内存,直接16G,然后,还是卡顿,CPU是i5的 ...
- Codeforces 897 A.Scarborough Fair-字符替换
A. Scarborough Fair time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- (6)C#项目结构
一.项目下Properites文件夹 Properties文件夹 定义你程序集的属性 项目属性文件夹 一般只有一个 AssemblyInfo.cs 类文件,用于保存程序集的信息,如名称,版本等,这些信 ...
- 安装mysql数据库图文教程
一.首先下载该版本的Mysql 5.5.28双击软件,弹出软件的安装界面如下 二.点击Next ,点击同意
- LCA【bzoj3364】 [Usaco2004 Feb]Distance Queries 距离咨询
Description 奶牛们拒绝跑马拉松,因为她们悠闲的生活无法承受约翰选择的如此长的赛道.因此约翰决心找一条更合理的赛道,他打算咨询你.此题的地图形式与前两题相同.但读入地图之后,会有K个问题. ...
- [BZOJ 2957]楼房重建(THU2013集训)(线段树维护单调栈)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2957 分析: 根据题意,就是比较斜率大小 只看一段区间的话,那么这段区间能看见的楼房数量就是这 ...
- [POI2014]Tourism
题目大意: 给定一个$n(n\le20000)$条个点,$m(m\le25000)$条边的无向图,保证图中最长路径上的点数不超过$10$.对一个点染色的代价是$w_i$.求使得每个结点都被染色或至少有 ...