iOS 读取相册信息保存到项目沙盒

小记:因为项目中使用到了录屏功能,所以采用了苹果自带的ReplayKit框架,由于项目框架的局限性,我们只能把录制好的视频保存到相册,即使我们拿到了 movieUrl,我们依然不能获取到数据进行本地保存,所以需要把相册中的数据读取到项目的沙盒,以便进行后续的操作。


读取相册信息

1 首先需要导入 #import
2 读取相册内容

PHFetchOptions *option = [[PHFetchOptions alloc] init];
    option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld",PHAssetMediaTypeVideo];
    option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
    
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString*phoneType = [NSString stringWithCString: systemInfo.machine encoding:NSASCIIStringEncoding];
    PHFetchResult *smartAlbums;
    
    if([phoneType  isEqualToString:@"iPhone7,2"]){
        smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
    }else{
        smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumVideos options:nil];
    }
    //fetchAssetCollectionsWithType
    // 苹果6 的取法
//    PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
//    // 非苹果 6 的取法
//    PHFetchResult *smartAlbumss =[PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumVideos options:nil];
    for (PHAssetCollection *collection in smartAlbums) {
        // 有可能是PHCollectionList类的的对象,过滤掉
        if (![collection isKindOfClass:[PHAssetCollection class]]) continue;
        // 过滤空相册
        if (collection.estimatedAssetCount <= 0) continue;
        if (collection) {
            PHAsset *phAsset;
            PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:collection options:option];
           // 取相册中最新写入的视频
            // // 苹果6 的做法
            if([phoneType  isEqualToString:@"iPhone7,2"]){
                phAsset = (PHAsset *)fetchResult.lastObject;
            }else{
                phAsset = (PHAsset *)fetchResult.firstObject;
            }
      // 通过 PHAsset 来获取视频的url地址、时长、大小等信息
    }


获取图片或者视频的信息

- (void)getVideoPathFromPHAsset:(PHAsset *)asset Complete:(void (^)(NSString *filePatch,NSString *dTime))result {
    PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
    options.version = PHImageRequestOptionsVersionCurrent;
    options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
    
    [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
        
        
        
        NSString * sandboxExtensionTokenKey = info[@"PHImageFileSandboxExtensionTokenKey"];
        
        NSArray * arr = [sandboxExtensionTokenKey componentsSeparatedByString:@";"];
        
        NSString * filePath = arr[arr.count - 1];
        
        CMTime   time = [asset duration];
        int seconds = ceil(time.value/time.timescale);
        NSString *str_minute = [NSString stringWithFormat:@"%d",seconds/60];
        NSString *str_second = [NSString stringWithFormat:@"%.2d",seconds%60];
        NSString *format_time = [NSString stringWithFormat:@"%@:%@",str_minute,str_second];
        // 判断 路径是不是存在,避免返回空的情况
        if (filePath) {
            result(filePath,format_time);
        }else{
            
        }
        
        
    }];
}

注:发现不同苹果手机的相册的目录结构是不一样的,和系统的版本无关,因为手头资源有限,这里只是测试了6,7,7p,x 。发现只有6的相册的目录结构和别的手机的不一样,其余的均相同,这里采用的6是iOS12.2。如有新的发现请留言相告。

参考:https://www.jianshu.com/p/f566e0ab96ef

你可能感兴趣的:(iOS 读取相册信息保存到项目沙盒)