iOS提供的AssetsLibrary框架是用来读取和操作本地相册的,连接了我们应用程序和相册之间的访问, 该框架提供一下6个类:ALAssetsLibrary,AssetsLibrary,ALAssetsGroup,ALAsset,ALAssetsFilter,ALAssetRepresentation。
ALAssetsLibrary类可以实现查看相册列表,增加相册,保存图片到相册等功能。
常用的几个方法:
1. 获得相册的组别(Get the list of groups that match the given types)
- (void)enumerateGroupsWithTypes:(ALAssetsGroupType)types usingBlock:(ALAssetsLibraryGroupsEnumerationResultsBlock)enumerationBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock;在这个方法的enumerationBlock中读取分组列表,包括它的名称、封面图片等等。
2. 返回一个ALAsset对象(Returns an ALAsset object in the result block for a URL previously retrieved from an ALAsset object)
- (void)assetForURL:(NSURL *)assetURL resultBlock:(ALAssetsLibraryAssetForURLResultBlock)resultBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock;
- (void)groupForURL:(NSURL *)groupURL resultBlock:(ALAssetsLibraryGroupResultBlock)resultBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock
没提供什么方法,就是一个包含其他五个类的一个头文件
ALAssetsGroup就是相册组的类,可以通过这个类提供的方法来获取相册组的各种信息。
1. 获得 名称,类型,ID,NSURL
- (id)valueForProperty:(NSString *)property;
property可以是:
ALAssetsGroupPropertyName(名称)
ALAssetsGroupPropertyType(类型)
ALAssetsGroupPropertyPersistentID(ID)
ALAssetsGroupPropertyURL (NSURL)
2. 获得相册分组的封面图片
- (CGImageRef)posterImage;
注意使用:
<span style="font-size:18px;">//获取相册group的封面 cell.imageView.image = [UIImage imageWithCGImage:[group posterImage]];</span>
- (void)setAssetsFilter:(ALAssetsFilter *)filter
ALAssetsFilter中有这些方法,是过滤类型
// Get all photos assets in the assets group. + (ALAssetsFilter *)allPhotos; // Get all video assets in the assets group. + (ALAssetsFilter *)allVideos; // Get all assets in the group. + (ALAssetsFilter *)allAssets;
- (NSInteger)numberOfAssets;
5. 通过相册组获取里面的图片:
- (void)enumerateAssetsUsingBlock:(ALAssetsGroupEnumerationResultsBlock)enumerationBlock; - (void)enumerateAssetsWithOptions:(NSEnumerationOptions)options usingBlock:(ALAssetsGroupEnumerationResultsBlock)enumerationBlock; - (void)enumerateAssetsAtIndexes:(NSIndexSet *)indexSet options:(NSEnumerationOptions)options usingBlock:(ALAssetsGroupEnumerationResultsBlock)enumerationBlock;
6. 增加图片
- (BOOL)addAsset:(ALAsset *)asset
上面说过了,提供了三种过滤方法:
// Get all photos assets in the assets group. + (ALAssetsFilter *)allPhotos; // Get all video assets in the assets group. + (ALAssetsFilter *)allVideos; // Get all assets in the group. + (ALAssetsFilter *)allAssets;
直接上代码说明问题。参考点击打开链接
//获取资源图片的详细资源信息 ALAssetRepresentation* representation = [asset defaultRepresentation]; //获取资源图片的长宽 CGSize dimension = [representation dimensions]; //获取资源图片的高清图 [representation fullResolutionImage]; //获取资源图片的全屏图 [representation fullScreenImage]; //获取资源图片的名字 NSString* filename = [representation filename]; NSLog(@"filename:%@",filename); //缩放倍数 [representation scale]; //图片资源容量大小 [representation size]; //图片资源原数据 [representation metadata]; //旋转方向 [representation orientation]; //资源图片url地址,该地址和ALAsset通过ALAssetPropertyAssetURL获取的url地址是一样的 NSURL* url = [representation url]; NSLog(@"url:%@",url); //资源图片uti,唯一标示符 NSLog(@"uti:%@",[representation UTI]);
照片类,这里要说的就是
- (id)valueForProperty:(NSString *)property;</span>
1.ALAssetPropertyType 资源的类型(照片,视频)
2.ALAssetPropertyLocation 资源地理位置(无位置信息返回null)
3.ALAssetPropertyDuation 播放时长(照片返回ALErorInvalidProperty)
得到缩略图
- (CGImageRef)thumbnail;
相册浏览器/选择器/照相机Demo:LGPhotoBrowser
记得给star哦