最近在做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) {
});
}
}];
}];
}
}