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的更多相关文章
随机推荐
- Linux screen命令
一.简介 GNU Screen是一款由GNU计划开发的用于命令行终端切换的自由软件.用户可以通过该软件同时连接多个本地或远程的命令行会话,并在其间自由切换. GNU Screen可以看作是窗口管理器的 ...
- Bad Hair Day
/* Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-c ...
- 2018年UI设计趋势概览
互联网产品的用户界面设计趋势是根据用户的不同需求而不断变化的.在仔细分析了过去几年用户界面设计的趋势和创新之后,我们可以发现其背后的一些规律,2018年UI界面设计的趋势如下. 渐变色 在过去的几年 ...
- VMware下的Linux系统中Windows的共享目录,不支持创建软连接
[问题] 在编译VMware下的Linux系统对从Windows中共享过来的文件,进行编译的时候,遇到: ln: creating symbolic link XXXXXX : Operation ...
- 使用PrintWriter out=response.getWriter();输出script脚本时乱码解决
使用PrintWriter out=response.getWriter();输出script脚本时乱码解决 最近遇到了一个奇怪的事情,仅仅用out.print("<script ty ...
- 2018.10.20 bzoj1068: [SCOI2007]压缩(区间dp)
传送门 这题转移很妙啊. f[l][r][1/0]f[l][r][1/0]f[l][r][1/0]表示对于区间[l,r][l,r][l,r]有/无重复的机会时压缩的最小值. 那么可以从三种情况转移过来 ...
- maven随笔
1.在我们项目顶层的POM文件中,我们会看到dependencyManagement元素.通过它元素来管理jar包的版本,让子项目中引用一个依赖而不用显示的列出版本号.Maven会沿着父子层次向上走, ...
- IntelliJ IDEA 2017版 spring-boot使用Spring Data JPA使用Repository<T, T>编程
1.环境搭建pom.xml搭建 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=& ...
- Windows10+Python3+BeautifulSoup4 安装
用正则表达式来提取网页中的内容是相当麻烦的,这里介绍一个可以从HTML或XML文件中提取数据的Python库:Beautiful Soup.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的 ...
- (匹配 二维建图) Antenna Placement --POJ --3020
链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82834#probl ...