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的更多相关文章
随机推荐
- 剑指offer面试题3二维数组中的查找
题目: 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 需要与面试官确认的是,这 ...
- python生成验证码,文字转换为图片-乾颐堂
在58或者赶集等一些网站上经常看到手机号是图片格式,或者一些网站的验证码.这些都是动态生成的,今天我们来看一下如何用python把文字生成图片.其实今天主要借助pygame的图像渲染模块,这样比较简单 ...
- tp5允许跨域
header("Access-Control-Allow-Origin: *"); 放在命名空间之后
- How to Install and Configure Bind 9 (DNS Server) on Ubuntu / Debian System
by Pradeep Kumar · Published November 19, 2017 · Updated November 19, 2017 DNS or Domain Name System ...
- 通过BeanShell获取UUID并将参数传递给Jmeter
有些HTTPS请求报文的报文体中包含由客户端生成的UUID,在用Jmeter做接口自动化测试的时候,因为越过了客户端,直接向服务器端发送报文,所以,需要在Jmeter中通过beanshell获取UUI ...
- APMServ—优秀的PHP集成环境工具
经常折腾wordpress和各种php开发的cms,免不了要在本地测试这些程序,所以选择一款好的php集成环境就至关重要啦.之前在月光博客上看到有一篇“常见的WAMP集成环境”介绍,然后先后试用过XA ...
- flex 分页
<?xml version="1.0" encoding="utf-8"?><s:Group xmlns:fx="http://ns ...
- 使用idea,springboot,springsession,redis实现分布式微服务的session 共享
本次开发环境:idea2016.3.4 +jdk1.8+maven3.3.9+redis+springboot+jedis 本文中的项目使用Maven来管理项目依赖,使用Spring Session和 ...
- linux 各项配置汇总
DNS配置 linux动态地址无需配置DNSlinux配置静态地址时,需要重新设置DNS,DNS的地址为:自己所用网络商的DNS地址,其中DNS地址还分区域例如:电信 江苏南京dns:218.2.13 ...
- python的特性
python使用c语言开发 1.面向对象的特性 面向对象的程序设计抽象出对象的行为和属性,把行为和属性分离开,但是又合理的组织在一起.它消除了保护类型.抽象类.接口等面向对象的元素,使得面向对象的概念 ...