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

随机推荐

  1. Oracle 基础表查询

    --查询所有用户表的数据 SELECT * FROM ALL_TABLES WHERE OWNER='USER_NAME' --如果是用该用户登录使用以下语句: SELECT * FROM USER_ ...

  2. 品味性能之道<三>:方法论

    自顶向下的性能优化方法论 系统优化是包括系统设计.开发.产品上线.平台优化的全过程,不同阶段的优化工作对全系统所带来的效益是不同的.理想的性能优化论应该采用自顶向下的优化方法,即在项目设计.开发和上线 ...

  3. [Sikuli] Sikuli安装

    http://blog.csdn.net/defectfinder/article/details/49819215 1.下载 sikulixsetup-1.1.0.jar (md5, sig) ht ...

  4. 在delphi XE5 里面编译kbmmw4.3

    Delphi XE5 仓促的发布了,虽然开始支持Android 开发了,但是经过试用,发现那个模拟器慢到无法用, 真机可以运行,但是调试也几乎无法用.由于XE5 的主要增加的是Android 的开发支 ...

  5. SQLInjection 靶场配置

    对于渗透,太小型的网站没有太大价值,而大型网站(比如各种电商平台)对于代码审计往往非常严格,新手基本找不到漏洞,而一些比较容易搞掉的站点(政府.gov.各种教育网站.edu或者很多商业中型站点)渗透又 ...

  6. 2018.08.22 NOIP模拟 shop(lower_bound+前缀和预处理)

    Shop 有 n 种物品,第 i 种物品的价格为 vi,每天最多购买 xi 个. 有 m 天,第 i 天你有 wi 的钱,你会不停购买能买得起的最贵的物品.你需要求出你每天会购买多少个物品. [输入格 ...

  7. UVa 1639 Candy (数学期望+组合数学+高精度存储)

    题意:有两个盒子各有n个糖,每次随机选一个(概率分别为p,1-p),然后吃掉,直到有一次,你打开盒子发现,没糖了! 输入n,p,求另一个盒子里糖的个数的数学期望. 析:先不说这个题多坑,首先要用lon ...

  8. python读取文件另存为

    fr = open(filename_r,encoding='cp852') w2 = open(filename_w,'a')#a代表追加 w代表重写 for line in fr: w2.writ ...

  9. 修改Python IDLE代码配色及语法高亮主题

    初学Python,想必大家拿来练习最多的IDE就是Python自带的IDLE了,但是默认的代码配色及语法高亮主题确实很不适应,所以我们需要做个小小的美化,比如像下面这样我做的美化配置: HOW TO ...

  10. C++中的矩阵运算

    C++中的矩阵运算 1. 2阶矩阵的逆矩阵公式