iOS之Photos:访问某个相册通过collectionView显示
文中相关知识点较多,只记载重点思路,相关部分都有对应注释说明,部分还需要优化,只是工作学习的一种思路。
@import AVFoundation;
@import Photos; 导入框架
- (void)viewDidLoad {
[super viewDidLoad];
[self getallAblumInfo];得到相应相册的所有资源
}
- (void)getallAblumInfo{
_localDic = [NSMutableDictionary dictionary];表示对应日期的相应资源集合的键值对
_allKeys = [NSMutableArray array];所有键的集合(目的是字典不会排序,需要通过键值获取对应时间升序的value数组)
PHFetchOptions *options = [[PHFetchOptions alloc]init];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];通过创建日期升序遍历
PHFetchResult *userCollections = [PHCollection fetchTopLevelUserCollectionsWithOptions:nil];获取用户创建的相册集合
for (PHCollection *c in userCollections) {
if ([c.localizedTitle isEqualToString:@"Test"]) { 条件:集合元数据的相册名为Test
PHAssetCollection *collection = (PHAssetCollection *)c;
PHFetchResult *results = [PHAsset fetchAssetsInAssetCollection:collection options:options];在test相册中通过options条件得到phasset资源集合
NSMutableArray *arr = [NSMutableArray array];
for (int i = 0; i < results.count; i++) {
按时间排序分组
PHAsset *asset1 = results[i];
NSString *str1 = [NSDateFormatter localizedStringFromDate:asset1.creationDate
dateStyle:NSDateFormatterMediumStyle
timeStyle:NSDateFormatterNoStyle];得到资源的年月日
if (i == 0) {
[_allKeys addObject:str1];
[arr addObject:asset1];
[_localDic setObject:arr forKey:str1];
}
if (i > 0) {
PHAsset *asset0 = results[i-1];
NSString *str0 = [NSDateFormatter localizedStringFromDate:asset0.creationDate
dateStyle:NSDateFormatterMediumStyle
timeStyle:NSDateFormatterNoStyle];
if ([str0 isEqualToString:str1]) {
[arr addObject:asset1];
[_localDic setObject:arr forKey:str1];添加元数据到字典
}else{
[_allKeys addObject:str1];
arr = [NSMutableArray array];
[arr addObject:asset1];
[_localDic setObject:arr forKey:str1];
}
}
}
}
}
}
条件2: uicollectionviewdatasource
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
LocalCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:LocalID forIndexPath:indexPath];
PHImageManager *manager = [PHImageManager defaultManager];
if (cell.tag) {
NSLog(@"tag = %ld",cell.tag);
[manager cancelImageRequest:(PHImageRequestID)cell.tag];
}
PHAsset *asset = [_localDic objectForKey:_allKeys[indexPath.section]][indexPath.row];
if (asset.mediaType == PHAssetMediaTypeImage) {资源为图片类型
PHImageRequestOptions *options = [[PHImageRequestOptions alloc]init];
options.resizeMode = PHImageRequestOptionsResizeModeFast;
options.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat;
options.synchronous = YES;
cell.tag = [manager requestImageForAsset:asset targetSize:CGSizeMake(300, 300) contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage *result, NSDictionary *info) {
cell.contentImg.image = result;获取缩略图
}];
}else if(asset.mediaType == PHAssetMediaTypeVideo){资源为视频类型
cell.tag = [manager requestAVAssetForVideo:asset options:nil resultHandler:^(AVAsset *asset1, AVAudioMix *audioMix, NSDictionary *info) {
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc]initWithAsset:asset1];
generator.appliesPreferredTrackTransform = YES;将图片设置为正图
CMTime time = CMTimeMake(0.6, 30);当前时刻的图片
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
UIImage *thumbImg = [UIImage imageWithCGImage:image];
if (result == AVAssetImageGeneratorSucceeded) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
UIGraphicsBeginImageContextWithOptions(cell.contentImg.image.size, NO, 0.0);主线程中合并图片,主要是给缩略图加上一个播放按钮图区分
[thumbImg drawInRect:CGRectMake(0, 0, cell.contentImg.image.size.width, cell.contentImg.image.size.height)];
[[UIImage imageNamed:@"Play"] drawInRect:CGRectMake(cell.contentImg.image.size.width/2-cell.contentImg.image.size.width/6, cell.contentImg.image.size.height/2-cell.contentImg.image.size.height/6, cell.contentImg.image.size.width/3, cell.contentImg.image.size.height/3)];
UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.contentImg.image = fullImage;
});
}
};
generator.maximumSize = CGSizeMake(200, 200);
[generator generateCGImagesAsynchronouslyForTimes:
[NSArray arrayWithObject:[NSValue valueWithCMTime:time]] completionHandler:handler];
}];
}
return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
HeaderCollectionView *reusable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
reusable.lab.text = _allKeys[indexPath.section];设置头视图的日期
NSLog(@"text : %@",reusable.lab.text);
return reusable;
}
UICollectionViewLayoutDelegate 设置头视图的高度(垂直滚动)
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
return CGSizeMake(self.view.bounds.size.width, 44);
}
iOS之Photos:访问某个相册通过collectionView显示的更多相关文章
- mui 访问系统相册将图片显示到网页
访问系统相返回值为一个对象,通过转换为字符串可以查看,path.files[0]为返回路径去除路径赋值到src 调用摄像头返回的相片的path为一个路径通过 plus.io.resolveLocalF ...
- [iOS 开发] app无法访问本地相册,且不显示在设置 -隐私 - 照片中
近几天在使用iOS8的Photos Framework访问本地相册时,app即不会弹出是否允许访问提示框,也无法显示在iPhone的设置-隐私-照片的访问列表中,代码如下: PHAuthorizati ...
- IOS编程之相机和相册
概述 IOS设备中的相机和相册,是我们在项目开发中经常会使用到的多媒体元素,使用相机可以获得最新想要的照片,而使用相册则可以访问IOS设备中的图片资源 使用IOS设备中的相机/相册获得图片资源 是否允 ...
- Android相机、相册获取图片显示并保存到SD卡
Android相机.相册获取图片显示并保存到SD卡 [复制链接] 电梯直达 楼主 发表于 2013-3-13 19:51:43 | 只看该作者 |只看大图 本帖最后由 happy小妖同学 ...
- C# 之 读取Word时发生 “拒绝访问” 及 “消息筛选器显示应用程序正在使用中” 异常的处理
1.Asp.net中建立Microsoft.Office.Interop.Word.Application时出现 “ 拒绝访问 ” 错误 项目中要实现在服务器端打开一个Word模版文件,修改其内容后再 ...
- 杂记-格式化Date默认格式,日期加一天,jstl判断字符类型,ajax模拟from表单后台跳转页面,jstl访问数据库并在页面显示
1.格式化Date默认格式 String str="Sun Oct 08 22:36:45 CST 2017"; SimpleDateFormat sdf = new Simple ...
- Delphi for iOS开发指南(8):在iOS应用程序中使用Tab组件来显示分页
Delphi for iOS开发指南(8):在iOS应用程序中使用Tab组件来显示分页 在FireMonkey iOS应用程序中的Tab Tab由FMX.TabControl.TTabControl定 ...
- 李洪强iOS开发之-实现点击单行View显示和隐藏Cell
李洪强iOS开发之-实现点击单行View显示和隐藏Cell 实现的效果: .... ....
- python统计apache、nginx访问日志IP访问次数并且排序(显示前20条)【转】
前言:python统计apache.nginx访问日志IP访问次数并且排序(显示前20条).其实用awk+sort等命令可以实现,用awk数组也可以实现,这里只是用python尝试下. apach ...
随机推荐
- Cookie案例
实现的功能是:在页面上点击,各种书名,跳转到另一个页面,在点解return 回到原来的页面,并且最下面出现刚才点击的书名:进行多次操作,都会出现刚刚点击的书名: 类似于:在浏览器上显示你登录的记录: ...
- Hiking 分类: 比赛 HDU 函数 2015-08-09 21:24 3人阅读 评论(0) 收藏
Hiking Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Subm ...
- 哈希-Snowflake Snow Snowflakes 分类: POJ 哈希 2015-08-06 20:53 2人阅读 评论(0) 收藏
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 34762 Accepted: ...
- python学习笔记-day4笔记 常用内置函数与装饰器
1.常用的python函数 abs 求绝对值 all 判断迭代器中所有的数据是否为真或者可迭代数据为空,返回真,否则返回假 any ...
- 最长递增子序列LIS再谈
DP模型: d(i) 以第 i 个元素结尾的最长递增子序列的长度. 那么就有 d(i) = max(d(j)) + 1;(j<i&&a[j]<a[i]),答案 max(d( ...
- Poj(1511),SPFA
题目链接:http://poj.org/problem?id=1511 嗯,最后一次写SPFA了,以后就套模板了. 题意:给出n个点和n条有向边,求所有点到源点1的来回最短路之和(保证每个点都可以往返 ...
- 驱动之module_init/module_exit
在前面helloworld的编写里面,我们使用了两个宏分别是module_init和module_exit,这里分析下为什么使用这两个宏. 在写模块的时候有两个特殊的函数,分别是init_module ...
- phpcms 01
1 安装完成phpcms,然后打开2 C:\wamp\www\phpcms\templates 复制下面的default 目录,改名为ypzy2014 3 修改ypzy2014文件夹下的config配 ...
- linux下查看电脑配置
1. 查看cpu ~$ cat /proc/cpuinfo 2. 查看内存占用 ~$ cat /proc/meminfo 3. 硬盘分区 $ cat /proc/partitions 4. ubunt ...
- php基础复习(3)文件上传于下载
一. 客户端配置: 表单页面 表单的发送方式是post 添加enctype = “multipart/form-data” <form action="doaction.php&q ...