加入 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;
}
“`