zoukankan      html  css  js  c++  java
  • 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];
  • 相关阅读:
    spring cache设置指定Key过期时间
    Idea Debug多线程不进断点问题处理
    spring cloud gateway使用 uri: lb://方式配置时,服务名的特殊要求
    信息学奥赛一本通(C++)在线评测系统——基础(一)C++语言——1067:整数的个数
    信息学奥赛一本通(C++)在线评测系统——基础(一)C++语言——1067:整数的个数
    征战蓝桥 —— 2013年第四届 —— C/C++A组第10题——大臣的旅费
    征战蓝桥 —— 2013年第四届 —— C/C++A组第10题——大臣的旅费
    征战蓝桥 —— 2013年第四届 —— C/C++A组第10题——大臣的旅费
    信息学奥赛一本通(C++)在线评测系统——基础(一)C++语言—— 1066:满足条件的数累加
    信息学奥赛一本通(C++)在线评测系统——基础(一)C++语言—— 1066:满足条件的数累加
  • 原文地址:https://www.cnblogs.com/jisa/p/9481032.html
Copyright © 2011-2022 走看看