ALAssetsLibrary

来源:互联网 发布:c语言n不为0的表达式 编辑:程序博客网 时间:2024/06/05 00:34
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 {

    [superviewDidLoad];

    

    [selfcreateData];

    self.tableView = [[UITableViewalloc] initWithFrame:self.view.boundsstyle:UITableViewStylePlain];

    self.tableView.delegate =self;

    self.tableView.dataSource =self;

    [self.viewaddSubview:self.tableView];

}


-(void)createData{

    self.assetsLibrary = [[ALAssetsLibraryalloc] init];

    self.groupArray = [[NSMutableArrayalloc] initWithCapacity:1];

    self.imageArray = [[NSMutableArrayalloc] initWithCapacity:1];

    [self.assetsLibraryenumerateGroupsWithTypes:ALAssetsGroupAllusingBlock:^(ALAssetsGroup *group,BOOL *stop) {

        if(group){

            [self.groupArrayaddObject:group];

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

            //eg  Name:柚子相机 typealbumassets count1

            NSLog(@"%@",group);

            

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

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

                if(result){

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

                    [self.imageArrayaddObject:image];

                    NSString *type = [result valueForProperty:ALAssetPropertyType];

                }

            }];

        }

        [self.tableViewreloadData];

    } 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 = [tableViewdequeueReusableCellWithIdentifier:@"id"];

    if(cell == nil){

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"id"];

    }

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

    return cell;

}


0 0
原创粉丝点击