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. 获取验证码效果和后台代码(js+html+cs)

    客户端js+html代码 <script type="text/javascript"> var tcode = 0;//定时器返回代码 //获得验证码 functio ...

  2. linux下给php安装memcached及memcache扩展(转)

    http://kimi.it/257.html (另外的方法)linux安装memcached及memcache扩展一.安装libevent函数库下载地址:http://libevent.org默认被 ...

  3. 数学整合 新(LUOGU)

    1.卡特兰数(P2532) 递推式:h(n)=C(2n,n)/(n+1) (n=0,1,2,...) 前十项(从零开始):1, 1, 2, 5, 14, 42, 132, 429, 1430, 486 ...

  4. Codeforces 631C. Report 模拟

    C. Report time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...

  5. Codeforces 658A. Robbers' watch 模拟

    A. Robbers' watch time limit per test: 2 seconds memory limit per test: 256 megabytes input: standar ...

  6. CComSafeArray

    https://blog.csdn.net/tangaowen/article/details/6554640

  7. Sketch小妙招:在线分享设计

    Sketch是很多产品经理.UI.UX设计师常使用的一款的设计工具,但是我在使用Sketch的过程中发现了一个让我困扰的事儿,或者说它缺少了一个我非常需要的服务:在线分享设计.可能很多使用Sketch ...

  8. 2018.10.14 loj#516. DP 一般看规律(启发式合并)

    传送门 注意到一种颜色改了之后就不能改回去了. 因此可以启发式合并. 每次把小的合并给大的. 这样每个数最多被合并logloglog次. 如果维护一棵比较下标的平衡树的话,对于答案有贡献的就是每个数与 ...

  9. 2018.09.27 codeforces1045A. Last chance(线段树优化建图+最大流)

    传送门 看完题应该都知道是网络流了吧. 但是第二种武器直接建图会gg. 因此我们用线段树优化建图. 具体操作就是,对于这m个人先建一棵线段树,父亲向儿子连容量为inf的边,最后叶子结点向对应的人连容量 ...

  10. 全国各地dns服务器列表

    211.103.13.101 江苏省无锡市 移动DNS服务器 211.136.28.231 北京市 移动DNS服务器 211.136.28.234 北京市 移动DNS服务器 211.136.28.23 ...