首页 > 代码库 > ios 相册操作 ALAssetsLibrary

ios 相册操作 ALAssetsLibrary

1.ALAssetsLibrary 实例为我们提供了获取相册(照片app)中的图片和视频的功能。在ios8 photos framework代替了ALAssetsLibrary

在使用ALAssetsLibrary时,我们需要申明它的实例。

ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];


2. 迭代获取相册ALAssetsGroup

- (void)enumerateGroupsWithTypes:(ALAssetsGroupType)types
                      usingBlock:(ALAssetsLibraryGroupsEnumerationResultsBlock)enumerationBlock
                    failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock

ALASSetsGroupType: 类型
  • ALAssetsGroupLibrary:从iTunes 来的相册内容(如本身自带的向日葵照片)。
  • ALAssetsGroupAlbum:设备自身产生或从iTunes同步来的照片,但是不包括照片流跟分享流中的照片。(例如从各个软件中保存下来的图片)
  • ALAssetsGroupEvent 相机接口事件产生的相册
  • ALAssetsGroupFaces 脸部相册(具体不清楚)
  • ALAssetsGroupSavedPhotos 相机胶卷照片
  • ALAssetsGroupPhotoStream 照片流
  • ALAssetsGroupAll   除了ALAssetsGroupLibrary上面所的内容。
ALAssetsLibraryGroupsEnumerationResultsBlock

    ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) {
    ALAssetsFilter *onlyPhotosFilter = [ALAssetsFilter allPhotos];
        [group setAssetsFilter:onlyPhotosFilter];
        if ([group numberOfAssets] > 0)
        {
            [self.imageGroup addObject:group];
        }
        else
        {
            [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
        }
    };

上面就是迭代AlAssetsGroup的block。每迭代一次就把相应的AlAssetsGroup保存在一个可变的数组之中。AlAssetsGroup中的一些属性表明了这个相册的特征。比如:

posterImage   相册的缩略图

numberOfAssets  相册中照片的数量



valueForProperty 通过NSString获取一些属性,有以下的属性
NSString*constALAssetsGroupPropertyName;NSString*constALAssetsGroupPropertyType;NSString*constALAssetsGroupPropertyPersistentID; (唯一标识ID)NSString*constALAssetsGroupPropertyURL;(唯一标识URL

#import "AssetsViewController.h"
#import  <AssetsLibrary/AssetsLibrary.h>
@interface AssetsViewController ()<UITableViewDataSource,UITableViewDelegate>

@property  (nonatomic, strong)  ALAssetsLibrary *assetsLibrary;
@property  (nonatomic, strong)  UITableView *tableView;
@property  (nonatomic, strong)  NSMutableArray *imageGroup;
@end

@implementation AssetsViewController

- (ALAssetsLibrary *)assetsLibrary
{
    if (!_assetsLibrary) {
        _assetsLibrary = [[ALAssetsLibrary alloc] init];
    }
    return _assetsLibrary;
}

- (NSMutableArray *)imageGroup
{
    if (!_imageGroup) {
        _imageGroup = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _imageGroup;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.imageGroup removeAllObjects];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:tableView];
    tableView.delegate = self;
    tableView.dataSource = self;
    
    ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) {
    ALAssetsFilter *onlyPhotosFilter = [ALAssetsFilter allPhotos];
        [group setAssetsFilter:onlyPhotosFilter];
        if ([group numberOfAssets] > 0)
        {
            [self.imageGroup addObject:group];
        }
        else
        {
            [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
        }
    };

    NSUInteger groupTypes = ALAssetsGroupAll ;
    
    [self.assetsLibrary enumerateGroupsWithTypes:groupTypes usingBlock:listGroupBlock failureBlock:^(NSError *error) {
        NSLog(@"Group not found!\n");
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.imageGroup.count;
}

-  (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellName = @"name";
    UITableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:cellName];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName];
    }
    
    ALAssetsGroup *group = [self.imageGroup objectAtIndex:indexPath.row];
    UIImage *postImage = [UIImage imageWithCGImage:[group posterImage]];
    
    cell.imageView.image =  postImage;
    cell.textLabel.text = [group valueForProperty:ALAssetsGroupPropertyName];;
    cell.detailTextLabel.text = [@(group.numberOfAssets) stringValue];
    return cell;
}









ios 相册操作 ALAssetsLibrary