标签:
关于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参数解析如下:
ALAssetsGroupLibrary // The Library group that includes all assets. 所有Asset类型
ALAssetsGroupAlbum // All the albums synced from iTunes or created on the device. 所有的专辑同步从iTunes或创建设备
ALAssetsGroupEvent // All the events synced from iTunes. 从iTunes同步
ALAssetsGroupFaces // All the faces albums synced from iTunes. iTunes专辑同步
ALAssetsGroupSavedPhotos // The Saved Photos album. 保存图片
#if __IPHONE_5_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
ALAssetsGroupPhotoStream // The PhotoStream album.
#endif
ALAssetsGroupAll // The same as ORing together all the available group types,所有组别
获得组别后 对组别进行操作:
//组封面
- (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属性:
– 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]]; } }];
标签:
原文地址:http://www.cnblogs.com/chaochaobuhuifei55/p/5413827.html