获取iPhone相册的照片

加入 AssetsLibrary.framework
导入头文件 #import

1.数据源,即所有相册的照片
if (!self.assets) {
    _assets = [[NSMutableArray alloc] init];
} else {
    [self.assets removeAllObjects];
}

ALAssetsGroupEnumerationResultsBlock assetsEnumerationBlock = ^(ALAsset *result, NSUInteger index, BOOL *stop) {

    if (result) {
        [self.assets addObject:result];
    }
};

ALAssetsFilter *onlyPhotosFilter = [ALAssetsFilter allPhotos];
[self.assetsGroup setAssetsFilter:onlyPhotosFilter];
[self.assetsGroup enumerateAssetsUsingBlock:assetsEnumerationBlock];

以 UICollectionViewController为例显示相册的照片

2.显示在CollectionView上
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {

    return self.assets.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"photoCell";

    UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    // load the asset for this cell
    ALAsset *asset = self.assets[indexPath.row];
    CGImageRef thumbnailImageRef = [asset thumbnail];
    UIImage *thumbnail = [UIImage imageWithCGImage:thumbnailImageRef];

    // apply the image to the cell
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:kImageViewTag];
    imageView.image = thumbnail;

    return cell;
}

获得的ALAsset对象就是相片对象:其中有相片的缩略图,全屏图,高清图,url等属性。

ALAsset *result = [assets objectAtIndex:index];

获取url:

String类型:

NSString *url = [[[result

                   defaultRepresentation]url]description];

URL类型:

NSURL *url = [[result defaultRepresentation]url];

获取缩略图:

CGImageRef  ref = [result thumbnail];

UIImage *img = [[UIImage alloc]initWithCGImage:ref];

获取全屏相片:

CGImageRef ref = [[result  defaultRepresentation]fullScreenImage];

UIImage *img = [[UIImage alloc]initWithCGImage:ref];

获取高清相片:

CGImageRef ref = [[result  defaultRepresentation]fullResolutionImage];

UIImage *img = [[UIImage alloc]initWithCGImage:ref];

获取相册的标示照片:

ALAssetsGroup *group = [groups objectAtIndex:index];

CGImageRef posterImageRef = [group posterImage];

UIImage *posterImage = [UIImage

                        imageWithCGImage:posterImageRef];


获取相册中的所有对象(相片和录像等):

group = (ALAssetsGroup*)[assetGroups objectAtIndex:index];

[group setAssetsFilter:[ALAssetsFilter allAssets]];

[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger

                                   index, BOOL *stop)

{

    if(result == nil)

    {

        return;

    }

    //获取相片的url:

    NSString *url = [[[result

                       defaultRepresentation]url]description];

    [self.urls addObject:url];

    [self.assets addObject:result];

}];
在tableview展示
 if (self.assetsLibrary == nil) {
        _assetsLibrary = [[ALAssetsLibrary alloc] init];
    }
    if (self.groups == nil) {
        _groups = [[NSMutableArray alloc] init];
    } 
    else
     {
           [self.groups removeAllObjects];
    }


    // setup our failure view controller in case enumerateGroupsWithTypes fails
    ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *error) {

        AssetsDataIsInaccessibleViewController *assetsDataInaccessibleViewController =
            [self.storyboard instantiateViewControllerWithIdentifier:@"inaccessibleViewController"];

        NSString *errorMessage = nil;
        switch ([error code]) {
            case ALAssetsLibraryAccessUserDeniedError:
            case ALAssetsLibraryAccessGloballyDeniedError:
                errorMessage = @"The user has declined access to it.";
                break;
            default:
                errorMessage = @"Reason unknown.";
                break;
        }

        assetsDataInaccessibleViewController.explanation = errorMessage;
        [self presentViewController:assetsDataInaccessibleViewController animated:NO completion:nil];
    };

    // emumerate through our groups and only add groups that contain photos
    ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) {

        ALAssetsFilter *onlyPhotosFilter = [ALAssetsFilter allPhotos];
        [group setAssetsFilter:onlyPhotosFilter];
        if ([group numberOfAssets] > 0)
        {
            [self.groups addObject:group];
        }
        else
        {
            [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
        }
    };

    // enumerate only photos
    NSUInteger groupTypes = ALAssetsGroupAlbum | ALAssetsGroupEvent | ALAssetsGroupFaces | ALAssetsGroupSavedPhotos;
    [self.assetsLibrary enumerateGroupsWithTypes:groupTypes usingBlock:listGroupBlock failureBlock:failureBlock];
}

// determine the number of rows in the table view
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return self.groups.count;

}

// determine the appearance of table view cells
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

ALAssetsGroup *groupForCell = self.groups[indexPath.row];
CGImageRef posterImageRef = [groupForCell posterImage];
UIImage *posterImage = [UIImage imageWithCGImage:posterImageRef];
cell.imageView.image = posterImage;
cell.textLabel.text = [groupForCell valueForProperty:ALAssetsGroupPropertyName];
cell.detailTextLabel.text = [@(groupForCell.numberOfAssets) stringValue];

return cell;
}

“`

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值