@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:柚子相机 type:album,assets count:1
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;
}