IOS相册视频写入到沙盒并分享

本文介绍了解决iOS系统中从相册分享视频至邮箱等问题的方法。包括如何查找相册及其中的文件、从相册获取视频并将其转换为可在沙盒环境中使用的格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在做IOS的系统分享,遇到了存储到相册里面的视频文件无法通过系统分享,分享到邮箱的问题,

为了解决这个问题,就需要把系统相册中的视频写入到沙盒里面,通过沙盒的url进行分享处理。

搜索的stackoverflow的网站,期间参考了很多人的代码,

目前做如下知识的总结,希望对其他人也有帮助:

1 寻找相册以及相册中的文件


   if(NSClassFromString(@"PHPhotoLibrary")){

            

            self.photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];

        }else{

            

            self.assetsLibrary = [[ALAssetsLibrary alloc] init];

        }


            

第一种方法,根据title进行搜索

PHFetchOptions *option = [[PHFetchOptions alloc] init];

            option.predicate = [NSPredicate predicateWithFormat:@"title = %@",fileName];

            

       PHFetchResult *collectionResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:option];


第二种,搜索相册打印列表获取 PHAssetCollection *collection

        PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];

        

        [userAlbums enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) {

            

            NSLog(@"album title %@", collection.localizedTitle);

      

          

        }];


      [self.assetsLibrary enumerateGroupsWithTypes:types usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

            if(group.numberOfAssets > 0){

               

            }

            

        } failureBlock:failureBlock];



2 从collection 到 PHAsset 或者  ALAsset

    if(self.assetCollection){

        PHFetchOptions *options = [PHFetchOptions new];

        options.predicate = [NSPredicate predicateWithFormat:@"mediaType = %i", PHAssetMediaTypeVideo];

        PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:self.assetCollection options:options];


        [result enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

           

        }];

        

    }else{

        [self.assetsGroup enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {

             

        }];

    }

从asset到相册的url

- (NSURL *)fileUrl

{

    NSDictionary *dict = [self.asset valueForProperty:ALAssetPropertyURLs];

    return [dict allValues].firstObject;

}

从相册的url转到沙盒的url,可以多个进行操作

 

    NSURL *outputURL = [NSURL fileURLWithPath:videoPath];

    

    AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:inputURL options:nil];

    AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset: urlAsset presetName:AVAssetExportPresetHighestQuality];

    session.outputURL = outputURL;

    session.outputFileType = AVFileTypeMPEG4;

    [session exportAsynchronouslyWithCompletionHandler:^(void){

        DDLogInfo(@"");

         操作成功返回url

    }];


另外还有一个从相册视频转移到沙盒的里面

PHFetchResult* fetchResult = [PHAsset fetchAssetsWithALAssetURLs:@[[self fileUrl]] options:nil];

    if (fetchResult.count > 0) {

        PHAsset *assetNew = [fetchResult firstObject];

        if (assetNew.mediaType == PHAssetMediaTypeVideo) {

         

            [[PHImageManager defaultManager] requestExportSessionForVideo:assetNew options:nil exportPreset:AVAssetExportPresetPassthrough resultHandler:^(AVAssetExportSession *exportSession, NSDictionary *info) {


                NSURL *outputURL = [NSURL fileURLWithPath:videoPath];

                NSLog(@"this is the final path %@",outputURL);

                exportSession.outputFileType=AVFileTypeMPEG4;

                exportSession.outputURL=outputURL;

                [exportSession exportAsynchronouslyWithCompletionHandler:^{

                    if (exportSession.status == AVAssetExportSessionStatusFailed) {

                        NSLog(@"failed");

                    } else if(exportSession.status == AVAssetExportSessionStatusCompleted){

                        NSLog(@"completed!");

                        dispatch_async(dispatch_get_main_queue(), ^(void) {

                            

                        });

                    }

                }];

            }];

        }

    }


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值