ALAssetsLibrary

ALAssetsLibrary提供了访问iOS设备下“照片”应用下所有照片和视频的接口;
从ALAssetsLibrary中可读取所有的相册数据,即ALAssetsGroup对象列表;
从ALAssetsGroup中可读取到其中包含的照片或视频列表,即ALAsset对象列表;

每个ALAsset可能有多个representations表示,即ALAssetRepresentation对象,使用其defaultRepresentation方法可获得其默认的representations,使用[assetValueForProperty:ALAssetPropertyRepresentations]可获取其所有representations的UTI数组。

从ALAsset对象可获取缩略图thumbnail 或 aspectRatioThumbnail;

从ALAsset对象可获取全尺寸图片(fullResolutionImage),全屏图片(fullScreenImage)及图片的各种属性:orientation,dimensions,scale,url,metadata等

其层次关系为 ALAssetsLibrary->ALAssetsGroup->ALAsset->ALAssetRepresentation




@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>


@property (nonatomic , strong)ALAssetsLibrary *assetsLibrary;

@property (nonatomic , strong)NSMutableArray *groupArray;

@property (nonatomic , strong)NSMutableArray *imageArray;

@property (nonatomic , strong)UITableView *tableView;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    [self createData];

    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];

    self.tableView.delegate = self;

    self.tableView.dataSource = self;

    [self.view addSubview:self.tableView];

}


-(void)createData{

    self.assetsLibrary = [[ALAssetsLibrary alloc] init];

    self.groupArray = [[NSMutableArray alloc] initWithCapacity:1];

    self.imageArray = [[NSMutableArray alloc] initWithCapacity:1];

    [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

        if(group){

            [self.groupArray addObject:group];

            //通过这个可以知道相册的名字 ,从而也可以知道安装的部分应用

            //eg  Name:柚子相机 typealbumassets count1

            NSLog(@"%@",group);

            

#pragma mark -- 获取相册中的相片

            [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {

                if(result){

                    UIImage *image = [UIImage imageWithCGImage:result.thumbnail];

                    [self.imageArray addObject:image];

                    NSString *type = [result valueForProperty:ALAssetPropertyType];

                }

            }];

        }

        [self.tableView reloadData];

    } failureBlock:^(NSError *error) {

        NSLog(@"Group not found!\n");

    }];

}


#pragma  mark -- UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.imageArray.count;

}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"id"];

    if(cell == nil){

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"id"];

    }

    cell.imageView.image = self.imageArray[indexPath.row];

    return cell;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值