一. ALAssetsLibrary 介绍

ALAssetsLibrary 提供了访问iOS设备下”照片”应用下所有照片和视频的接口;

  1. 从 ALAssetsLibrary 中可读取所有的相册数据,即 ALAssetsGroup 对象列表;
  2. 从每个 ALAssetsGroup 中可获取到其中包含的照片或视频列表,即 ALAsset 对象列表;
  3. 每个 ALAsset 可能有多个representations表示,即 ALAssetRepresentation 对象,使用其 defaultRepresentation 方法可获得其默认representations,使用[asset valueForProperty: ALAssetPropertyRepresentations ]可获取其所有representations的 UTI 数组。
  4. 从ALAsset对象可获取缩略图 thumbnail 或 aspectRatioThumbnail ;
  5. 从 ALAssetRepresentation 对象可获取全尺寸图片( fullResolutionImage),全屏图片( fullScreenImage )及图片的各种属性: orientation , dimensions , scale , url , metadata 等。

其层次关系为 ALAssetsLibrary -> ALAssetsGroup -> ALAsset -> ALAssetRepresentation 。

注意:

  1. 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

  2. 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.

  3. 系统”相册”程序显示的图片是 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的各种属性

    1. 相册封面图片 [assetsGroup posterImage ];
    2. 照片url[[[asset defaultRepresentation] url] absoluteString];
    3. 照片缩略图 
      [asset thumbnail]; 
      [asset aspectRatioThumbnail];
    4. 照片全尺寸图 
      [[asset defaultRepresentation] fullResolutionImage];
    5. 照片全屏图 
      [[asset defaultRepresentation] fullScreenImage];
    6. 照片创建时间 
      [asset valueForProperty:ALAssetPropertyDate];
    7. 照片拍摄位置(可能为nil) 
      [asset valueForProperty:ALAssetPropertyLocation];
    8. 照片尺寸 
      [[asset defaultRepresentation] dimensions];

转载自:http://www.tuicool.com/articles/UBZJFb

使用ALAssetsLibrary读取所有照片的更多相关文章

  1. iOS---如何获取手机的本地照片和相册

    __weak ViewController *weakSelf = self; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIO ...

  2. 如何读取Access里的OLE类型的图片

    身份证一类读卡器读取的照片信息,保存在Access数据库中一般为OLE型字段,图片为BMP格式,因为是用其读卡器写入的,其数据类型为常二进制数据. 再用报表或EXCEL读取这些图片时,如果将该图片字段 ...

  3. iOS开发之保存照片到自己创建的相簿

    iOS开发之保存照片到自己创建的相簿 保存照片还可以用ALAssetsLibrary,ALAssetsLibrary提供了我们对iOS设备中的相片.视频的访问,是连接应用程序和相册之间访问的一个桥梁. ...

  4. Java读取本地文件,并显示在JSP文件中

        当我们初学IMG标签时,我们知道通过设置img标签的src属性,能够在页面中显示想要展示的图片.其中src的值,可以是磁盘目录上的绝对,也可以是项目下的相对路径,还可以是网络上的图片路径.在存 ...

  5. qt之数据库对照片的存取

    需要确保数据库连接上 QOCI为驱动 //oracle 数据库连接 //需要在执行文件目录添加 oci.dll oraociei11.dll QSqlDatabase db = QSqlDatabas ...

  6. [Xcode 实际操作]九、实用进阶-(12)从系统相册中读取图片

    目录:[Swift]Xcode实际操作 本文将演示从系统相册中读取图片. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //添加两个协议 ...

  7. S1的小成果:MyKTV系统

    转眼之间,已经到了2016年,即新的一年了!S1也结束了,收获的也不多 ,想想最后留给大家的就一个KTV项目了. 希望大家看时有所收获           现在我们一起来看KTV前台管理 主界面的运行 ...

  8. 【转】Windows Phone在隔离存储里存取图片文件

    一共两个页面,第一个为MainPage.xaml,代码如下: <!--ContentPanel - place additional content here--> <Grid x: ...

  9. 利用canvas实现抽奖转盘---转载别人的

    功能需求 转盘要美观,转动效果流畅. 转盘上需要显示奖品图片,并且奖品是后台读取的照片和名字. 转动动画完成后要有相应提示. 获取的奖品具体算法在数据库里操作,前端只提供最后的效果展示.   知识要点 ...

随机推荐

  1. scandir函数详解

    scandir函数详解2009-10-30 10:51scandir函数:读取特定的目录数据表头文件:#include <dirent.h>定义函数:int scandir(const c ...

  2. Appium+python自动化13-native和webview切换【转载】

    前言 现在大部分app都是混合式的native+webview,对应native上的元素通过uiautomatorviewer很容易定位到,webview上的元素就无法识别了. 一.识别webview ...

  3. hdu 5104(数学)

    Primes Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. [loj#101] 最大流 网络流模板

    #101. 最大流 内存限制:512 MiB时间限制:5000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据   题目描述 这是一道模板题. 给定  ...

  5. navicat连接MySQL8.0.11提示2059错误

    错误原因:mysql加密规则的改变: mysql加密规则:mysql_native_password      mysql8之前的版本   caching_sha2_password     mysq ...

  6. SpringBoot动态数据源

    1.原理图 2.创建枚举类 /** * 存数据源key值 */ public enum DataSourceKey { master,salve,migration } 3.创建自定义注解类 /** ...

  7. 【BZOJ2276】Temperature

    题面 Description The Byteotian Institute of Meteorology (BIM) measures the air temperature daily. The ...

  8. nat的翻译类型(1)--静态nat

    目的:在1.1 1.2 1.3 三台内网的服务器访问外网的服务器(202.1.1.2)时,将内网ip转换为外网ip. 1.设置内网三台服务器的Ip ,网关,以及外网服务器的ip网关 分别为:192.1 ...

  9. iOS中正则表达式的基本使用方法

    一.第三方框架RegexKitLite的使用 在ios项目中可以借用第三方框架RegexKitLite来简化对正则表达式的使用,使用方法如下 1.去RegexKitLite下载类库,解压出来会有一个例 ...

  10. 在CcentOS系统上将deb包转换为rpm包

    deb文件格式本是ubuntu/debian系统下的安装文件,那么我想要在redhat/centos/fedora中安装,需要把deb格式的软件包转化成rpm格式. 需要用到的转换工具:alien_8 ...