博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[iOS] photoKit获取所有照片
阅读量:4309 次
发布时间:2019-06-06

本文共 1737 字,大约阅读时间需要 5 分钟。

代码:

- (NSMutableArray *)getAllPhoto{    NSMutableArray *arr = [NSMutableArray array];        // 所有智能相册    PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];    for (NSInteger i = 0; i < smartAlbums.count; i++) {        PHCollection *collection = smartAlbums[i];        //遍历获取相册        if ([collection isKindOfClass:[PHAssetCollection class]]) {            PHAssetCollection *assetCollection = (PHAssetCollection *)collection;            PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];            PHAsset *asset = nil;            if (fetchResult.count != 0) {                for (NSInteger j = 0; j < fetchResult.count; j++) {                    //从相册中取出照片                    asset = fetchResult[j];                    PHImageRequestOptions *opt = [[PHImageRequestOptions alloc]init];                    opt.synchronous = YES;                    PHImageManager *imageManager = [[PHImageManager alloc] init];                    [imageManager requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFill options:opt resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {                        if (result) {                            [arr addObject:result];                        }                    }];        }            }    }        }        //返回所有照片    return arr;

由于此方法为同步方法 所以需要放在子线程中去执行 例如:

dispatch_async(dispatch_get_global_queue(0, 0), ^{        NSMutableArray *arr = [self getAllPhoto];        NSLog(@"完成%@ \n照片总数%ld", arr, arr.count);    });​

转载于:https://www.cnblogs.com/iwgo/p/5549731.html

你可能感兴趣的文章
<a4j:keeyAlive>的英文介绍
查看>>
关于list对象的转化问题
查看>>
VOPO对象介绍
查看>>
suse创建的虚拟机,修改ip地址
查看>>
linux的挂载的问题,重启后就挂载就没有了
查看>>
docker原始镜像启动容器并创建Apache服务器实现反向代理
查看>>
docker容器秒死的解决办法
查看>>
管理网&业务网的一些笔记
查看>>
openstack报错解决一
查看>>
openstack报错解决二
查看>>
linux source命令
查看>>
openstack报错解决三
查看>>
乙未年年终总结
查看>>
子网掩码
查看>>
第一天上班没精神
查看>>
启动eclipse报错:Failed to load the JNI shared library
查看>>
eclipse安装插件的两种方式在线和离线
查看>>
linux下源的相关笔记(suse)
查看>>
linux系统分区文件系统划分札记
查看>>
Linux(SUSE 12)安装Tomcat
查看>>