ALAssetsgroup

来源:互联网 发布:虚拟固定电话软件 编辑:程序博客网 时间:2024/04/29 07:20

ALAssetsgroup

---------------------------------------------------------------------------

Enumerating Assets(遍历资源)

  • – enumerateAssetsUsingBlock:(用一个block来遍历组里的资源)
  • – enumerateAssetsWithOptions:usingBlock:(在枚举选项的条件下,用一个block来遍历组里的资源
  • – enumerateAssetsAtIndexes:options:usingBlock:(在枚举选项的条件下,用一个block来遍历组里特定index的资源

Adding Assets(添加资源)

  • – addAsset:(添加一个已存在的asset到接收者。返回yes成功;反之,失败。)
  •   editable  property(指示程序是否可以编辑组,只读属性,打印看了一下,系统自带的是不能编辑的,其它可以编辑)

Filtering(过滤)

  • – numberOfAssets(返回组过滤器条件下的资源个数,若没有设置过滤器,则返回组里的资源个数)
  • – setAssetsFilter:(设置组的过滤器)

Accessing Properties(访问属性)

  • – valueForProperty:(通过组属性名称,获取组属性:组名称,组类型,组永久性ID,组URL)
  • – posterImage(组的封面)



ALAsset

---------------------------------------------------------------------------

Asset Properties

  • – valueForProperty:
  •   (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)

Accessing Representations

  • – defaultRepresentation
  • – representationForUTI:
  • – thumbnail(小正方形的缩略图)
  • – aspectRatioThumbnail(按原始资源长宽比例的缩略图)

Setting New Image and Video Data

  • – setImageData:metadata:completionBlock: 
  • 用给定的image data 替换接收者的image data。

  • – setVideoAtPath:completionBlock: 
  • 用给定的URL的video 替换接收者的video data。

Saving to the Saved Photos Album

  • – writeModifiedImageDataToSavedPhotosAlbum:metadata:completionBlock:
  • 保存image data到Saved Photos album
  • – writeModifiedVideoAtPathToSavedPhotosAlbum:completionBlock: 

 保存video到Saved Photos album的指定路径


ALAssetRepresentation

---------------------------------------------------------------------------

        ALAssetRepresentation对象封装了一个给定ALAsset对象的陈述。

        一个在资源库中给定的asset可能有不止一个陈述。比如,如果一个相机提供RAW和JPEG格式的图像版本,

asset将有两个陈述版本,一个是RAW的,一个是JPEG的。

Getting Image Representations

  • – CGImageWithOptions:
  • – fullResolutionImage(完全分辨率的图片)
  • – fullScreenImage(满屏的图片)

Getting Image Attributes

  • – orientation(文件方向)
  • – scale(长宽比例)
  • – filename(文件名字)

Getting Raw Data

  • – size(文件尺寸,以byte为单位)
  • – getBytes:fromOffset:length:error:

Getting Metadata

  • – UTI
  • – metadata

Getting an URL

  • – url

使用

---------------------------------------------------------------------------


       利用ALAssetsLibrary来获取相册的分组:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. -(void)getGroup  
  2. {  
  3.     @autoreleasepool  
  4.     {  
  5.         ALAssetsLibraryAccessFailureBlock failureblock =  
  6.         ^(NSError *myerror)  
  7.         {  
  8.             NSLog(@"相册访问失败 =%@", [myerror localizedDescription]);  
  9.             if ([myerror.localizedDescription rangeOfString:@"Global denied access"].location!=NSNotFound) {  
  10.                 NSLog(@"无法访问相册.请在'设置->定位服务'设置为打开状态.");  
  11.             }else{  
  12.                 NSLog(@"相册访问失败.");  
  13.             }  
  14.         };  
  15.           
  16.         ALAssetsLibraryGroupsEnumerationResultsBlock  
  17.         libraryGroupsEnumeration = ^(ALAssetsGroup* group,BOOL* stop)  
  18.         {  
  19.             if (group!=nil)  
  20.             {  
  21.                 [self.groupArray addObject:group];  
  22.             }  
  23.             else  
  24.             {  
  25.                 if (!_groupTable)  
  26.                 {  
  27.                     _groupTable = [[UITableView alloc] initWithFrame:EZRECT(0, 0, SCREEN_SIZE_WIDTH, SCREEN_SIZE_HEIGHT - 44)  
  28.                                                                style:UITableViewStylePlain];  
  29.                     _groupTable.delegate = self;  
  30.                     _groupTable.dataSource = self;  
  31.                     [self.view addSubview:_groupTable];  
  32.                 }  
  33.                 [_groupTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];  
  34.             }  
  35.         };  
  36.         [[DataCenter defaultAssetsLibrary] enumerateGroupsWithTypes:ALAssetsGroupAll  
  37.                                                          usingBlock:libraryGroupsEnumeration  
  38.                                                        failureBlock:failureblock];  
  39.     }  
  40. }  


       由于分组在添加到groupArray中,后面要在外面的函数中调用,这样会发生警告,所以,将ALAssetsLibrary的获取写成一个单例
[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. + (ALAssetsLibrary *)defaultAssetsLibrary  
  2. {  
  3.     static dispatch_once_t pred = 0;  
  4.     static ALAssetsLibrary *library = nil;  
  5.     dispatch_once(&pred,  
  6.                   ^{  
  7.                       library = [[ALAssetsLibrary alloc] init];  
  8.                   });  
  9.     return library;  
  10. }  


     利用获得的分组再来获取资源文件:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. -(void)filterImageWithGroup:(ALAssetsGroup *)group  
  2. {  
  3.     [self.images removeAllObjects];  
  4.     ALAssetsGroupEnumerationResultsBlock groupEnumerAtion =  
  5.     ^(ALAsset *result,NSUInteger index, BOOL *stop)  
  6.     {  
  7.         if (result!=NULL)  
  8.         {  
  9.             if ([[result valueForProperty:ALAssetPropertyType]isEqualToString:ALAssetTypePhoto])  
  10.             {  
  11.                 [self.images addObject:result];  
  12.             }  
  13.         }  
  14.         else  
  15.         {  
  16.             //主线程中刷新UI  
  17.         }  
  18.           
  19.     };  
  20.     [group enumerateAssetsUsingBlock:groupEnumerAtion];  

0 0
原创粉丝点击