查了很多资料才知道目前的IOS版本不支持相册里面视频文件的分享,需要将视频文件转存到沙盒里面,
通过沙盒的url进行分享,以下分享转换的代码,留存处理。
将PHAsset格式的文件转存到沙盒里面,并返回分享的url。
-(void)getPHAssetUrl:(void(^)(NSURL* url))UrlBlk{
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) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yy_MM_dd_HH:mm"];
NSString *fileName = [NSString stringWithFormat:@"LocalRecord_%@.mp4",[formatter stringFromDate:[NSDate date]]];
NSString* videoPath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSMutableArray *videoList = [[NSMutableArray alloc] initWithCapacity:100];
for (NSString *file in fileList) {
if ([file rangeOfString:@"LocalRecord_"].location != NSNotFound) {
[videoList addObject:file];
}
}
DDLogInfo(@"videoList %@",videoList);
for (NSString *deleteFileName in videoList) {
NSString *deletaPath = [NSString stringWithFormat:@"%@/%@",documentsDirectory,deleteFileName];
DDLogInfo(@"deletaPath %@",deletaPath);
NSError *error;
BOOL success = [manager removeItemAtPath:deletaPath error:&error];
DDLogInfo(@"error is %@ seccessy is %d",error,success);
}
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) {
UrlBlk(outputURL);
});
}
}];
}];
}
}
}
直接获取PHAsset文件的url,打印出来显示是私有的文档,此url不能够被UIActivityViewController分享,不做使用,假如以后苹果支持视频分享了,可以用此方法进行分享。
PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
options.version = PHImageRequestOptionsVersionCurrent;
options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
PHImageManager *manager = [PHImageManager defaultManager];
[manager requestAVAssetForVideo:result.asset options:options resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
AVURLAsset *urlAsset = (AVURLAsset *)asset;
NSURL *url = urlAsset.URL;
// NSData *data = [NSData dataWithContentsOfURL:url];
[weakSelf.list addObject:url];
NSLog(@"%@",urlAsset.URL);
}];
搜索相册里面文件并获取的流程:
PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];
[userAlbums enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) {
NSLog(@"album title %@", collection.localizedTitle);
PHFetchOptions *options = [PHFetchOptions new];
options.predicate = [NSPredicate predicateWithFormat:@"mediaType = %i", PHAssetMediaTypeVideo];
PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:collection options:options];
[result enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
PHAsset *asset = (PHAsset*)obj;
}];
}];