PHPhotos
PHPhotoLibrary:
@abstract A PHPhotoLibrary provides access to the metadata and image data for the photos, videos and related content in the user's photo library, including content from the Camera Roll, iCloud Shared, Photo Stream, imported, and synced from iTunes. PHAssetCollectionChangeRequest can only be created or used within a -[PHPhotoLibrary performChanges:] or -[PHPhotoLibrary performChangesAndWait:] block. 提供了对照片库中的图片、视频等数据的访问。 主要是用户对图库使用权限的判断。以及执行相册的创建删除,图片的添加移除,监听等
/// 请求授权
- (void)FFAuthorization {
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusNotDetermined) {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { }];
}else if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied) {
NSAssert(YES, @"没有获得相册授权");
}
}
/**
创建或者获取图库以App的名字
*/
- (PHAssetCollection *)FF_CreateOrAcquireAppAlbum{
if ([PHPhotoLibrary authorizationStatus] != PHAuthorizationStatusAuthorized) {
return nil;
}
NSString *appDisplayName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
NSError *error = nil;
PHAssetCollection *collec = [self FF_AcqurieAutoAblumWithName:appDisplayName];
if (collec) {
return collec;
}
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
[PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:appDisplayName];
// 删除相册
// [PHAssetCollectionChangeRequest deleteAssetCollections:@[相册名称]]; } error:&error];
if (error) {
NSLog(@"自定义相册创建失败");
}else {
NSLog(@"相册创建成功");
}
return [self FF_AcqurieAutoAblumWithName:appDisplayName]; } /**
获取自定义的相册
*/
- (PHAssetCollection *)FF_AcqurieAutoAblumWithName:(NSString *)name {
PHFetchResult<PHAssetCollection *> *ablumList = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
for (PHAssetCollection *collec in ablumList) {
if ([collec isKindOfClass:[PHAssetCollection class]] && [collec.localizedTitle isEqualToString:name]) {
return collec;
}
}
return nil;
}
/**
保存图片
*/
- (void)FF_SaveImageToSpeciallyAlbum:(UIImage *)image { PHAssetCollection *ablum = [self FF_CreateOrAcquireAppAlbum];
NSError *error = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
// PHAssetChangeRequest can only be created or used within a -[PHPhotoLibrary performChanges:] or -[PHPhotoLibrary performChangesAndWait:] block.
PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
PHObjectPlaceholder *object = [assetRequest placeholderForCreatedAsset];
NSLog(@"%@", object.localIdentifier);
PHAssetCollectionChangeRequest *collectionRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:ablum];
[collectionRequest addAssets:@[object]];
} error:&error];
if (error) {
NSLog(@"图片添加失败");
}
}
/**
删除图片 根据下标
*/
- (void)FF_DeleteSpeciallyImage:(NSInteger)index {
PHAssetCollection *ablum = [self FF_CreateOrAcquireAppAlbum];
PHFetchResult<PHAsset *> *list = [PHAsset fetchAssetsInAssetCollection:ablum options:nil];
if (list.count <= index) {
return;
}
PHAsset *asset = list[index];
NSError *error = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
[PHAssetChangeRequest deleteAssets:@[asset]];
} error:&error];
if (error) {
NSLog(@"第%ld张图片删除失败", (long)index);
}
}
// 获取相册。
PHFetchResult *smartResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:nil];// 获取自定义的相册和从itunes同步过来的相册
// PHAssetCollectionTypeAlbum
// 系统内置的只能相册
// PHAssetCollectionTypeSmartAlbum
// 以时间划分的相册
// PHAssetCollectionTypeMoment
// 获取相册里的图片
PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
option.synchronous = YES;
PHFetchResult<PHAsset *> *assetList = [PHAsset fetchAssetsInAssetCollection:@"相册" options:option];
PHPhotos的更多相关文章
随机推荐
- 一个方便的java分页算法
一个好用的java分页算法,代码如下,只需要分页参数继承Pageable类就可以很方便分页了 package cn.com.base.common.pagination; /** * 分页基类 * * ...
- Rakefile实例教程
一.简介 简单的说Rakefile就是使用Ruby语法的makefile, 对应make的工具就是rake. 在Ruby on Rails里面, 不管是数据库的初始化, 内容初始化, 删除, 还是测试 ...
- c++ tricks
1 关于virtual关键字的实验 1.1 在派生类中改变virtual函数访问权限 定义两个类A,B,其中B公有派生于A.A中定义一个private成员虚函数func,B中覆写此函数,但是将其访问权 ...
- php调用window系统自带的命令,比如计算器
1.在cmd命令行输入calc.exe 2.
- Ubuntu 网卡多个 IP 地址
临时添加 IP 地址 首先,让我们找到网卡的 IP 地址.在我的 Ubuntu 15.10 服务器版中,我只使用了一个网卡. 运行下面的命令找到 IP 地址: 复制代码 代码如下: sudo ip a ...
- 【Maven】安装及配置(Linux)
本文介绍Linux环境下安装Maven 安装环境和软件 系统:Linux(CentOS) 软件:apache-maven-3.3.9-bin.tar.gz(解压版). 安装步骤 maven是基于Jav ...
- 2018.10.15 NOIP训练 百事世界杯之旅(期望dp)
传送门 期望题. 其实跟dpdpdp关系并不大. 考虑f[i]f[i]f[i]表示已经凑出了iii个需要的次数. 显然有:f[i]=ni∗f[i]+nn−i∗f[i+1]+1f[i]=\frac {n ...
- 2018.09.06 警卫安排(树形dp)
描述 太平王世子事件后,陆小凤成了皇上特聘的御前一品侍卫. 皇宫以午门为起点,直到后宫嫔妃们的寝宫,呈一棵树的形状:有边直接相连的宫殿可以互相望见.大内保卫森严,三步一岗,五步一哨,每个宫殿都要有人全 ...
- 2018.09.01 独立集(树形dp)
描述 给定一颗树(边权为1),选取一个节点子集,使得该集合中任意两个节点之间的距离都大于K.求这个集合节点最多是多少 输入 第一行是两个整数N,K 接下来是N-1行,每行2个整数x,y,表示x与y有一 ...
- Java 继承关系中:static,构造函数,成员变量的加载顺序
首先看下面的例子: package simple.demo; /** * @author Administrator * @date 2019/01/03 */ public class ClassA ...