AssetsLibrary虽然在iOS8以后已经被取代,但是现在做项目还需要兼容iOS7,所以还是用了它,记录如下。
#import "ViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
@interface ImageItem : NSObject
@property (nonatomic, assign) BOOL bChecked;
@property (nonatomic, strong) ALAsset *asset;
@end
@implementation ImageItem
@end
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
- (IBAction)openBtnAction:(id)sender;
@property (nonatomic, strong) ALAssetsLibrary *assetsLibrary;
@property (nonatomic, strong) NSMutableArray *groups;
@property (nonatomic, strong) NSMutableArray *assets;
@property (nonatomic, strong) ALAssetsGroup *assetsGroup;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if (self.assetsLibrary == nil) {
_assetsLibrary = [[ALAssetsLibrary alloc] init];
}
if (self.groups == nil) {
_groups = [[NSMutableArray alloc] init];
} else {
[self.groups removeAllObjects];
}
if (self.assets == nil) {
_assets = [[NSMutableArray alloc] init];
} else {
[self.assets removeAllObjects];
}
[self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
[self.groups addObject:group];
NSLog(@"%@",group);
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result) {
NSLog(@"%@",result);
ImageItem *item = [[ImageItem alloc] init];
item.bChecked = NO;
item.asset = result;
[self.assets addObject:item];
[self.collectionView reloadData];
}
}];
}
} failureBlock:^(NSError *error) {
NSLog(@"Group not found!\n");
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)openBtnAction:(id)sender {
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.collectionView reloadData];
}
#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return self.assets.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"photoCell";
UICollectionViewCell *cell;
cell = [cv dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
// load the asset for this cell
ImageItem *item;
item = self.assets[indexPath.row];
CGImageRef thumbnailImageRef = [item.asset thumbnail];
UIImage *thumbnail = [UIImage imageWithCGImage:thumbnailImageRef];
// apply the image to the cell
UIImageView *iv1;
UIImageView *iv2;
iv1 = (UIImageView *)[cell viewWithTag:1];
iv2 = (UIImageView *)[cell viewWithTag:2];
iv1.image = thumbnail;
if (item.bChecked) {
iv2.image = [UIImage imageNamed:@"checkbox_pressed"];
}else{
iv2.image = [UIImage imageNamed:@"checkbox_normal"];
}
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(80, 80);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(10,10,10,10);//UIEdgeInsetsZero;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"%ld",indexPath.item);
ImageItem *item;
item = self.assets[indexPath.row];
if (item.bChecked) {
item.bChecked = NO;
}else{
item.bChecked = YES;
}
[collectionView reloadData];
}
@end
运行结果: