代码改变世界

ALAssetsLibrary学习总结

2015-02-09 09:59  JG2014  阅读(309)  评论(0编辑  收藏  举报

添加AssetsLibrary.framework

然后引入

#import <AssetsLibrary/ALAssetsLibrary.h>

一个获取所有图片的类

#import <UIKit/UIKit.h>
#include<AssetsLibrary/AssetsLibrary.h> 

@interface ViewController : UIViewController{
  ALAssetsLibrary *library;
  
  NSArray *imageArray;
  
  NSMutableArray *mutableArray;
}
-(void)allPhotosCollected:(NSArray*)imgArray;
@end
#import "ViewController.h"

@interface ViewController ()

@end

static int count=0;

@implementation ViewController

- (void)viewDidLoad
{
  [super viewDidLoad];
  [self getAllPictures];
}

//获取所有图片
-(void)getAllPictures

{
  imageArray=[[NSArray alloc] init];
  mutableArray =[[NSMutableArray alloc]init];
  NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];
  library = [[ALAssetsLibrary alloc] init];
  void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
    if(result != nil) {
      if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
        [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];
        NSURL *url= (NSURL*) [[result defaultRepresentation]url];
        
        [library assetForURL:url
         
             resultBlock:^(ALAsset *asset) {
               
               [mutableArray addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];

               if ([mutableArray count]==count)
                 
               {
                 
                 imageArray=[[NSArray alloc] initWithArray:mutableArray];
                 
                 [self allPhotosCollected:imageArray];
                 
               }
               
             }
            failureBlock:^(NSError *error){ NSLog(@"operation was not successfull!"); } ];
      }
      
    }
    
  };

  NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
  void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
    
    if(group != nil) {
      
      [group enumerateAssetsUsingBlock:assetEnumerator];
      
      [assetGroups addObject:group];
      
      count=[group numberOfAssets];
      
    }
    
  };
  assetGroups = [[NSMutableArray alloc] init];
  [library enumerateGroupsWithTypes:ALAssetsGroupAll
   
               usingBlock:assetGroupEnumerator
   
             failureBlock:^(NSError *error) {NSLog(@"There is an error");}];
  
}



-(void)allPhotosCollected:(NSArray*)imgArray
{
  //write your code here after getting all the photos from library...
  NSLog(@"all pictures are %@",imgArray);
}


@end

以下是关于AssetsLibrary的一些方法说明,仅供参考

//初始化,这个简单
if (!self.assetsLibrary) {
_assetsLibrary = [[ALAssetsLibrary alloc] init];
}
//添加一个group,成功后查看一些信息
[_assetsLibrary addAssetsGroupAlbumWithName:@"tesddt" resultBlock:^(ALAssetsGroup *group) {
//查看相册的名字
NSLog(@"ALAssetsGroupPropertyName:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
//查看相册的类型
NSLog(@"ALAssetsGroupPropertyType:%@",[group valueForProperty:ALAssetsGroupPropertyType]);
//查看相册的存储id
NSLog(@"ALAssetsGroupPropertyPersistentID:%@",[group valueForProperty:ALAssetsGroupPropertyPersistentID]);
//查看相册存储的位置地址
NSLog(@"ALAssetsGroupPropertyURL:%@",[group valueForProperty:ALAssetsGroupPropertyURL]);
} failureBlock:^(NSError *error) {

}];

可以通过valueForProperty获取到图片的信息,包括类型, Location , 时长,方向,日期,格式 , URL地址。

NSString * nsALAssetPropertyType = [ asset valueForProperty:ALAssetPropertyType ] ;

NSString * nsALAssetPropertyLocation = [ asset valueForProperty:ALAssetPropertyLocation ] ;

NSString * nsALAssetPropertyDuration = [ asset valueForProperty:ALAssetPropertyDuration ] ;

NSString * nsALAssetPropertyOrientation = [ asset valueForProperty:ALAssetPropertyOrientation ] ;

NSString * nsALAssetPropertyDate = [ asset valueForProperty:ALAssetPropertyDate ] ;

NSString * nsALAssetPropertyRepresentations = [ asset valueForProperty:ALAssetPropertyRepresentations ] ;

NSString * nsALAssetPropertyURLs = [ asset valueForProperty:ALAssetPropertyURLs ] ;

NSString * nsALAssetPropertyAssetURL = [ asset valueForProperty:ALAssetPropertyAssetURL ] ;

//通过url地址在相册资源中获取该地址的资源文件ALAsset,有可能是相片或视频
[_library assetForURL:[NSURL URLWithString:@""] resultBlock:^(ALAsset *asset) {


//     NSString *const ALAssetPropertyType;
//     NSString *const ALAssetPropertyLocation;
//     NSString *const ALAssetPropertyDuration;
//     NSString *const ALAssetPropertyOrientation;
//     NSString *const ALAssetPropertyDate;
//     NSString *const ALAssetPropertyRepresentations;
//     NSString *const ALAssetPropertyURLs;
//     NSString *const ALAssetPropertyAssetURL;



//查看资源的地理位置信息
NSLog(@"ALAssetPropertyLocation:%@",[asset valueForProperty:ALAssetPropertyLocation]);
//如果资源是视频,查看视频的时长
NSLog(@"ALAssetPropertyDuration:%@",[asset valueForProperty:ALAssetPropertyDuration]);
//查看资源的方向,图片的旋转方向
NSLog(@"ALAssetPropertyOrientation:%@",[asset valueForProperty:ALAssetPropertyOrientation]);
//查看资源的创建时间
NSLog(@"ALAssetPropertyDate:%@",[asset valueForProperty:ALAssetPropertyDate]);
//查看资源的描述信息
NSLog(@"ALAssetPropertyRepresentations:%@",[asset valueForProperty:ALAssetPropertyRepresentations]);
NSLog(@"ALAssetPropertyURLs:%@",[asset valueForProperty:ALAssetPropertyURLs]);
//查看资源的url路径
NSLog(@"ALAssetPropertyAssetURL:%@",[asset valueForProperty:ALAssetPropertyAssetURL]);
} failureBlock:^(NSError *error) {

}];
//通过url地址获取相册资源中的一个相册
[_library groupForURL:groupURL resultBlock:^(ALAssetsGroup *group) {
NSLog(@"ALAssetsGroupPropertyName:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
} failureBlock:^(NSError *error) {

}];
//根据选择的类型遍历相册资源中的相对应类型的所有相册,其中stop行参是指针,表示是否停止迭代,当赋值为false则停止

//        enum {
//        ALAssetsGroupLibrary        = (1 << 0),
//        ALAssetsGroupAlbum          = (1 << 1),
//        ALAssetsGroupEvent          = (1 << 2),
//        ALAssetsGroupFaces          = (1 << 3),
//        ALAssetsGroupSavedPhotos    = (1 << 4),
//        ALAssetsGroupPhotoStream    = (1 << 5),
//        ALAssetsGroupAll            = 0xFFFFFFFF,
//        };

[_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
NSLog(@"group name:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
} failureBlock:^(NSError *error) {

}];
//保存图片到系统默认的相册中,使用nsdata的形式,并返回照片的url地址
[_library writeImageDataToSavedPhotosAlbum:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {

}];
//保存图片到系统默认的相册中,使用cgimageref的形式,并返回照片的url地址
[_library writeImageToSavedPhotosAlbum:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {

}];
//保存图片到系统默认的相册中,使用cgimageref的形式,并且选择图片以什么旋转方向的形式保存,并返回照片的url地址

//        typedef enum {
//        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
//        } ALAssetOrientation;

UIImage* image = [UIImage imageNamed:@"test.png"];
[_library writeImageToSavedPhotosAlbum:[image CGImage] orientation:ALAssetOrientationLeft completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(@"save image:%@",assetURL);
}];