文中相关知识点较多,只记载重点思路,相关部分都有对应注释说明,部分还需要优化,只是工作学习的一种思路。

@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显示的更多相关文章

  1. mui 访问系统相册将图片显示到网页

    访问系统相返回值为一个对象,通过转换为字符串可以查看,path.files[0]为返回路径去除路径赋值到src 调用摄像头返回的相片的path为一个路径通过 plus.io.resolveLocalF ...

  2. [iOS 开发] app无法访问本地相册,且不显示在设置 -隐私 - 照片中

    近几天在使用iOS8的Photos Framework访问本地相册时,app即不会弹出是否允许访问提示框,也无法显示在iPhone的设置-隐私-照片的访问列表中,代码如下: PHAuthorizati ...

  3. IOS编程之相机和相册

    概述 IOS设备中的相机和相册,是我们在项目开发中经常会使用到的多媒体元素,使用相机可以获得最新想要的照片,而使用相册则可以访问IOS设备中的图片资源 使用IOS设备中的相机/相册获得图片资源 是否允 ...

  4. Android相机、相册获取图片显示并保存到SD卡

    Android相机.相册获取图片显示并保存到SD卡 [复制链接]   电梯直达 楼主    发表于 2013-3-13 19:51:43 | 只看该作者 |只看大图  本帖最后由 happy小妖同学 ...

  5. C# 之 读取Word时发生 “拒绝访问” 及 “消息筛选器显示应用程序正在使用中” 异常的处理

    1.Asp.net中建立Microsoft.Office.Interop.Word.Application时出现 “ 拒绝访问 ” 错误 项目中要实现在服务器端打开一个Word模版文件,修改其内容后再 ...

  6. 杂记-格式化Date默认格式,日期加一天,jstl判断字符类型,ajax模拟from表单后台跳转页面,jstl访问数据库并在页面显示

    1.格式化Date默认格式 String str="Sun Oct 08 22:36:45 CST 2017"; SimpleDateFormat sdf = new Simple ...

  7. Delphi for iOS开发指南(8):在iOS应用程序中使用Tab组件来显示分页

    Delphi for iOS开发指南(8):在iOS应用程序中使用Tab组件来显示分页 在FireMonkey iOS应用程序中的Tab Tab由FMX.TabControl.TTabControl定 ...

  8. 李洪强iOS开发之-实现点击单行View显示和隐藏Cell

    李洪强iOS开发之-实现点击单行View显示和隐藏Cell 实现的效果:  .... ....

  9. python统计apache、nginx访问日志IP访问次数并且排序(显示前20条)【转】

    前言:python统计apache.nginx访问日志IP访问次数并且排序(显示前20条).其实用awk+sort等命令可以实现,用awk数组也可以实现,这里只是用python尝试下.   apach ...

随机推荐

  1. 跨站点脚本编制实例(AppScan扫描结果)

    最近工作要求解决下web的项目的漏洞问题,扫描漏洞是用的AppScan工具,其中有很多是关于跨站点脚本编制问题的.下面就把这块东西分享出来. 原创文章,转载请注明 ------------------ ...

  2. Jams倒酒(pour)

    1.Jams倒酒(pour) Jams是一家酒吧的老板,他的酒吧提供2种体积的啤酒,a ml 和 b ml,分别使用容积为a ml 和 b ml的酒杯来装载. 酒吧的生意并不好.Jams发现酒鬼们都很 ...

  3. linux 强大的编辑器之vi

    vi编辑器是一个处理ASCII数据的文本工具.大多数linux发行版都已经默认安装了vi编辑器.vi是visual interface的缩写vim是 visual interface improved ...

  4. MVC api json 格式

    输出json var formatters = config.Formatters.Where(formatter => formatter.SupportedMediaTypes.Where( ...

  5. 工欲善其事必先利其器系列之:在VS里面折叠js代码

    之前用vs写js的时候经常因为js代码过长而且不能像cs文件里面的方法一样进行折叠而抓狂,直到在扩展库发现了这款插件有了它就可以把代码进行折叠了 插件地址

  6. UICollectionView 使用

    /** 初始化UICollectionView */ UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc ...

  7. CentOS7安装Docker,运行Nginx镜像、Centos镜像

    摘要 总体思路:yum命令直接安装Docker,下载想要的镜像并启动 1.环境,CentOS7 Minimal 64位,Docker必须要64位的系统 2.通过yum命令直接安装,yum instal ...

  8. LTMP手动编译安装以及全自动化部署实践(附详细代码)

    大家使用LNMP架构,一般可以理解为Linux Shell为CentOS/RadHat/Fedora/Debian/Ubuntu/等平台安装LNMP(Nginx/MySQL /PHP),LNMPA(N ...

  9. SUSE Linux Enterprise Server 设置防火墙开启ssh远程端口

    1.vi /etc/sysconfig/SuSEfirewall2   #编辑防火墙设置 FW_SERVICES_EXT_TCP="22"   #开启22端口 rcSuSEfire ...

  10. <转>如何改变讨好型人格 | 你根本不需要讨好任何人

    在我过去二十多年的生命里一直是一个“讨好者”. 我总是活在别人对我的期待中,我总是不停的追逐着别人对我的认可,我总是像个卑微的奴才一样去满足别人的需求. 但就和大多数的“讨好者”一样,我们越是寻求别人 ...