AssetsLibrary显示本地照片

本文介绍了一个基于AssetsLibrary的iOS应用实例,演示了如何枚举并展示用户相册中的图片。考虑到iOS 8之前的版本兼容性问题,此代码仍采用AssetsLibrary而非Photos框架。文章详细展示了如何创建图片项、加载图片以及实现简单的选中效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

运行结果:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值