关于ALAssetsLibrary的简单使用有两个方面:

第一:存储图片/视频方法如下:

// With a UIImage, the API user can use -[UIImage CGImage] to get a CGImageRef, and cast -[UIImage imageOrientation] to ALAssetOrientation.
- (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef orientation:(ALAssetOrientation)orientation completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock;
// The API user will have to specify the orientation key in the metadata dictionary to preserve the orientation of the image
- (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock // If there is a conflict between the metadata in the image data and the metadata dictionary, the image data metadata values will be overwritten
- (void)writeImageDataToSavedPhotosAlbum:(NSData *)imageData metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock
//
- (void)writeVideoAtPathToSavedPhotosAlbum:(NSURL *)videoPathURL completionBlock:(ALAssetsLibraryWriteVideoCompletionBlock)completionBlock

简单说明:

orientation为储存图片时的具体操作:

typedef NS_ENUM(NSInteger, ALAssetOrientation) {
    ALAssetOrientationUp                 // default orientation 默认方向
    ALAssetOrientationDown             // 180 deg rotation
    ALAssetOrientationLeft              // 90 deg CCW
    ALAssetOrientationRight               // 90 deg CW
    ALAssetOrientationUpMirrored          // as above but image mirrored along other axis. horizontal flip 镜像
    ALAssetOrientationDownMirrored      // horizontal flip
    ALAssetOrientationLeftMirrored        // vertical flip
    ALAssetOrientationRightMirrored      // vertical flip
}

metadata为元数据内容,如果内容和图片内部冲突,将覆盖操作;

第二:获取图片/视频

以下方法中是异步获取相册内容:

// Get the list of groups that match the given types. Multiple types can be ORed together. The results are passed one by one to the caller by executing the enumeration block.
// When the enumeration is done, 'enumerationBlock' will be called with group set to nil.
// When groups are enumerated, the user may be asked to confirm the application's access to the data. If the user denies access to the application or if no application is allowed to access the data, the failure block will be called.
// If the data is currently unavailable, the failure block will be called.
- (void)enumerateGroupsWithTypes:(ALAssetsGroupType)types usingBlock:(ALAssetsLibraryGroupsEnumerationResultsBlock)enumerationBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock

ALAssetsGroupType参数解析如下:

ALASSetsGroupType: 类型 ALAssetsGroupLibrary:从iTunes 来的相册内容。

ALAssetsGroupAlbum:设备自身产生或从iTunes同步来的照片,但是不包括照片流跟分享流 中的照片。(例如从各个软件中保存下来的图片)

ALAssetsGroupEvent 相机接口事件产生的相册
ALAssetsGroupFaces
脸部相册(具体不清楚)
ALAssetsGroupSavedPhotos
相机胶卷照片
ALAssetsGroupPhotoStream
照片流
ALAssetsGroupAll
除了ALAssetsGroupLibrary上面所的内容。

获得组别后 对组别进行操作:

//组封面
- (CGImageRef)posterImage
//组别个数
- (NSInteger)numberOfAssets
//约束
- (void)setAssetsFilter:(ALAssetsFilter *)filter
//枚举
- (void)enumerateAssetsUsingBlock:(ALAssetsGroupEnumerationResultsBlock)enumerationBlock
- (void)enumerateAssetsWithOptions:(NSEnumerationOptions)options usingBlock:(ALAssetsGroupEnumerationResultsBlock)enumerationBlock
- (void)enumerateAssetsAtIndexes:(NSIndexSet *)indexSet options:(NSEnumerationOptions)options usingBlock:(ALAssetsGroupEnumerationResultsBlock)enumerationBlock

参数说明:

ALAssetsFilter:

+ (ALAssetsFilter *)allPhotos;
+ (ALAssetsFilter *)allVideos;
+ (ALAssetsFilter *)allAssets;

NSEnumerationOptions:正反排序

NSIndexSet:组中第N个

对于ALAsset的说明:

方法:

  - (CGImageRef)thumbnail

  - (CGImageRef)aspectRatioThumbnail

  - (ALAssetRepresentation *)defaultRepresentation

  Representation属性:

  – CGImageWithOptions:

  – fullResolutionImage(完全分辨率的图片)

  – fullScreenImage(满屏的图片

  - (id)valueForProperty:(NSString *)property

property:

    1.ALAssetPropertyType 资源的类型(照片,视频)

      2.ALAssetPropertyLocation 资源地理位置(无位置信息返回null)

      3.ALAssetPropertyDuation 播放时长(照片返回ALErorInvalidProperty)

4.ALAssetPropertyOrientation 方向(共有8个方向,参见:ALAssetOrientation)

5.ALAssetPropertyDate 拍摄时间(包含了年与日时分秒)

6.ALAssetPropertyRepresentations 描述(打印看了下,只有带后缀的名称)

7.ALAssetPropertyURLs(返回一个字典,键值分别是文件名和文件的url)

8.ALAssetPropertyAssetURL 文件的url )

editable  property(指示资源是否可以编辑,只读属性)

originalAsset  property(原始资源。若没有保存修改后资源,则原始资源为nil)

我的代码:

-(void)getAllPicturesAndVideo
{
//失败控制
ALAssetsLibraryAccessFailureBlock failureblock =
^(NSError *myerror)
{
NSLog(@"相册访问失败 =%@", [myerror localizedDescription]);
if ([myerror.localizedDescription rangeOfString:@"Global denied access"].location!=NSNotFound) {
NSLog(@"无法访问相册.请在'设置->定位服务'设置为打开状态.");
}else{
NSLog(@"相册访问失败.");
}
return ;
};
mutableArray =[[NSMutableArray alloc]init];
  //成功
ALAssetsLibraryGroupsEnumerationResultsBlock libraryGroupsEnumeration =
  ^(ALAssetsGroup* group,BOOL* stop){
   //说明枚举结束用group==nil标识
if (group!=nil)
{
[mutableArray addObject:group];
}
else
{
NSLog(@"%ld",mutableArray.count);
        
//更新UI
}
};
[[ViewController defaultAssetsLibrary] enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:libraryGroupsEnumeration
failureBlock:failureblock]; }
//从组别中获取第index个数据
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:index] options:NSEnumerationConcurrent usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
//图片
if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) { UIImage *image=[UIImage imageWithCGImage:[result aspectRatioThumbnail]];
      
  }
     //视频
     else if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]){
//UIImage *image=[UIImage imageWithCGImage:[result aspectRatioThumbnail]];
       [[WHViewController defaultAssetsLibrary] assetForURL:[result valueForProperty:ALAssetPropertyAssetURL] resultBlock:^(ALAsset *asset) {
                //写入沙盒
                [self handleWrittenFile:asset];
            } failureBlock:^(NSError *error) {
                //
            }];
}
}];
//文件写入方法
-(void)handleWrittenFile:(ALAsset*) videoAsset
{
NSString *DocRoot = [(NSArray*)NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:];
NSString *tempFile = [NSString stringWithFormat:@"%@/VideoFileName.mov",DocRoot];
ALAssetRepresentation *rep = [videoAsset defaultRepresentation];
NSUInteger size = [rep size];
const int bufferSize = ;
NSLog(@"written to : %@", tempFile);
FILE *f = fopen([tempFile cStringUsingEncoding:], "wb+");
if (f==NULL) {
return;
}
Byte *buffer =(Byte*)malloc(bufferSize);
NSInteger read =, offset = ;
NSError *error;
if (size != ) {
do {
read = [rep getBytes:buffer
fromOffset:offset
length:bufferSize
error:&error];
fwrite(buffer, sizeof(char), read, f);
offset += read;
NSLog(@"read : %ld total : %ld",read, offset);
} while (read != );
}
fclose(f);
free(buffer);
}

ios ALAssetsLibrary简单的使用的更多相关文章

  1. iOS上简单推送通知(Push Notification)的实现

    iOS上简单推送通知(Push Notification)的实现 根据这篇很好的教程(http://www.raywenderlich.com/3443/apple-push-notification ...

  2. iOS CAReplicatorLayer 简单动画

    代码地址如下:http://www.demodashi.com/demo/11601.html 写在最前面,最近在看学习的时候,偶然间发现一个没有用过的Layer,于是抽空研究了下,本来应该能提前记录 ...

  3. iOS之简单瀑布流的实现

    iOS之简单瀑布流的实现   前言 超简单的瀑布流实现,这里说一下笔者的思路,详细代码在这里. 实现思路 collectionView能实现各中吊炸天的布局,其精髓就在于UICollectionVie ...

  4. iOS,手势识别简单使用

    1.iOS目前支持的手势识别(6种) 2.点按手势和慢速拖动手势简单使用 iOS目前支持的手势识别(6种) UITapGestureRecognizer(点按) UIPinchGestureRecog ...

  5. iOS NSData简单解析

    iOS 基本数据类型之NSData 1 nsdata 作用: 用于存储二进制的数据类型 nadat类提供一种简单的方式,它用来设置缓存区.将文件的内容读入到缓存区.或者将缓存区中的内容写到一个文件. ...

  6. iOS 多线程 简单学习NSThread NSOperation GCD

    1:首先简单介绍什么叫线程 可并发执行的,拥有最小系统资源,共享进程资源的基本调度单位. 共用堆,自有栈(官方资料说明iOS主线程栈大小为1M,其它线程为512K). 并发执行进度不可控,对非原子操作 ...

  7. iOS开发-简单工厂模式

    设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性.概念很长,iOS开发中最常 ...

  8. ios label 简单的长按复制文本信息

    在iOS开发过程中,有时候会用到UILabel展示的内容,那么就设计到点击UILabel复制它上面展示的内容的功能,也就是Label长按复制功能.网上有很多种给Label添加长按复制功能的方法,这里我 ...

  9. iOS 多线程简单使用的具体解释

    主线程 一个iOS程序执行后.默认会开启1条线程,称为"主线程"或"UI线程"(刷新UI界面最好在主线程中做.在子线程中可能会出现莫名其妙的BUG) 主线程的作 ...

随机推荐

  1. 2017-3-2 C#基础 集合

    要使用集合必须先引用命名空间,using System.Collections; 集合与数组的不同: 数组:同一类型,固定长度集合:不同类型,不固定长度 集合主要分为六大类:普通集合,泛型集合,哈希表 ...

  2. querySelectorAll与getElementsBy对比有什么不同

    querySelectorAll与getElementsBy对比有什么不同javascript中的querySelectorAll与getElementsBy都可以获取dom元素对象,但是他们又有什么 ...

  3. Java虚拟机(JVM)

    Java虚拟机 Java字节码通过类加载器(Class Loader)为程序的执行加载所需要的全部类.在类的加载过程中,由于是按照先加载启动类库.再加载扩展类库最后加载用户自定义类库的顺序,从而避免一 ...

  4. ajax bookstrap美化网页,并实现页面的加载,删除与查看详情

    Bookstrap:美化页面: Bootstrap是Twitter推出的一个开源的用于前端开发的工具包.它由Twitter的设计师Mark Otto和Jacob Thornton合作开发,是一个CSS ...

  5. 浅谈jquery插件开发模式

    首先根据<jQuery高级编程>的描述来看,jQuery插件开发方式主要有三种: 通过$.extend()来扩展jQuery 通过$.fn 向jQuery添加新的方法 通过$.widget ...

  6. git remote log error

    使用git pull的时候收到以下信息: error: there are still refs under 'refs/remotes/origin/xxxx'From 10.1.25.57:yyy ...

  7. dfs 无向图两节点间的所有路径

    标题:风险度量 X星系的的防卫体系包含 n 个空间站.这 n 个空间站间有 m 条通信链路,构成通信网.两个空间站间可能直接通信,也可能通过其它空间站中转. 对于两个站点x和y (x != y), 如 ...

  8. Tomcat+Eclipse乱码问题解决方法

    概述 乱码问题是大家在日常开发过程中经常会遇到的问题,由于各自环境的不同,解决起来也费时费力,本文主要介绍一般性乱码问题的解决方法与步骤,开发工具采用Eclipse+Tomcat,统一设置项目编码UT ...

  9. JavaScript实现

    JavaScript实现 Javascript实现虽然JavaScript和ECMAScript通常都被人们用来表达相同的含义,但JavaScript的含义却比ECMA-262中规定的要多得多.没错, ...

  10. css颜色代码对照

    FFFFFF #DDDDDD #AAAAAA #888888 #666666 #444444 #000000 #FFB7DD #FF88C2 #FF44AA  #FF0088  #C10066  #A ...