加入 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 cellALAsset *asset = self.assets[indexPath.row];CGImageRef thumbnailImageRef = [asset thumbnail];UIImage *thumbnail = [UIImage imageWithCGImage:thumbnailImageRef];// apply the image to the cellUIImageView *imageView = (UIImageView *)[cell viewWithTag:kImageViewTag];imageView.image = thumbnail;return cell;
}

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

ALAsset *result = [assets objectAtIndex:index];获取url:String类型:NSString *url = [[[resultdefaultRepresentation]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 = [UIImageimageWithCGImage:posterImageRef];获取相册中的所有对象(相片和录像等):group = (ALAssetsGroup*)[assetGroups objectAtIndex:index];[group setAssetsFilter:[ALAssetsFilter allAssets]];[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUIntegerindex, BOOL *stop){if(result == nil){return;}//获取相片的url:NSString *url = [[[resultdefaultRepresentation]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 failsALAssetsLibraryAccessFailureBlock 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 photosALAssetsLibraryGroupsEnumerationResultsBlock 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 photosNSUInteger 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;
}

“`

获取iPhone相册的照片相关推荐

  1. 获取IPhone相册中图片的方法(包括获取所有图片)

    获取iphone相册方法: 方法一: ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *gr ...

  2. 获取IPhone相册中图片的方法(ASSets)

    获取iphone相册方法: 方法一: ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *gr ...

  3. SwiftUI——如何获取系统相册的照片

    SwiftUI没办法自己获取系统相册内容,所以得依靠一下UIKit,头文件如下: import SwiftUI import UIKit 首先我们创建一个ImagePicker结构体供我们获取系统相册 ...

  4. 获取手机相册中照片的地址,保存在手机中的URL(Swift 代码)

    1.第一步打开 相册,TZImagePickerController 这个框架之前讲过,不细说了 let vc = TZImagePickerController(maxImagesCount: 1, ...

  5. iOS/swift之获取系统所有相册和照片录像、封装相册多选

    参考:http://www.hangge.com/blog/cache/detail_1512.html ios14 使用PHPicker获取相册(需要 import PhotosUI) 支持多选 支 ...

  6. iphone相册照片路径_如何使用相册整理iPhone照片

    iphone相册照片路径 Over time, your iPhone's Camera Roll has probably filled up, making it harder to find i ...

  7. WPF触控程序开发(三)——类似IPhone相册的反弹效果

    原文: WPF触控程序开发(三)--类似IPhone相册的反弹效果 用过IPhone的都知道,IPhone相册里,当图片放大到一定程度后,手指一放,会自动缩回,移动图片超出边框后手指一放,图片也会自动 ...

  8. Android获取系统相册图片选中地址,获取手机中的所有图片地址自定义相册

    一.获取手机中的值 1.首先在使用读写sd卡权限 2.获取手机中的所有图片: 注意代码中的getGalleryPhotos(getContentResolver()) 方法获取所有地址 获取所有图片地 ...

  9. 安卓开发———打开相机拍照或者打开相册选择照片并显示出来

    打开相机 布局代码 <Buttonandroid:id="@+id/take_photo"android:layout_width="match_parent&qu ...

最新文章

  1. Tomcat软件的目录结构、作用
  2. struts2 过滤器和拦截器
  3. java连接数据库核心代码
  4. ospf和pat及nat的配置
  5. 利用ListView实现新闻客户端的新闻内容图文混排
  6. C#内存映射文件学习总结
  7. 2020-11-24(dll注入的N种搞法)
  8. 吴恩达机器学习--单变量线性回归【学习笔记】
  9. ubuntu云主机上Flask+uwsgi+Nginx配置
  10. mac系统快捷键大全详细介绍
  11. argb可以和rgb同步吗_神光同步酷炫幻彩,安钛克光棱120 RGB风扇套装
  12. 商标申请的企业法律服务可避免法律风险
  13. scala List源码
  14. 55种数据可视化开源工具_8种出色的开源数据可视化工具
  15. 关于泊松过程的参数估计
  16. Java中IO流知识点总结
  17. php开发 文件下载,php 实现文件下载
  18. fork()和多线程
  19. 单片机开发,主函数没调用任何程序,串口一直收到00 0 FF之类的数据
  20. 2018DeeCamp笔试题目第一套B卷

热门文章

  1. 命令与征服3 凯恩之怒
  2. ftime()函数的用法!
  3. 百度地图上定位自己所在的位置
  4. 计算机毕业论文怎样写系统的意义,毕业论文写作的目的意义及步骤-计算机论文...
  5. 有个程序猿很忧桑:一个命令rm -rf/ ,他把整个公司删没了...
  6. uniapp开发h5微信授权登录(详细教程)
  7. inno setup 修改将程序安装路径写入注册表
  8. 个税不省,何以暴富?程序员汇算清缴注意事项和要点
  9. 错误:未报告的异常错误,必须对其进行捕获或声明以便抛出
  10. 什么是MBTI,16种人格类型详解