注:上邊文正提到過在蘋果6上取相冊視頻地址谬擦,取不到的情況,針對上篇文章在iOS12之后的缺陷朽缎,做出了如下的調(diào)整
代碼截圖.jpg
詳細(xì)代碼
//獲取最近一次的相冊圖片/視頻的信息PHAsset
PHFetchOptions *options = [[PHFetchOptions alloc] init];
// 進(jìn)行排序
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *assetsFetchResults = [PHAsset fetchAssetsWithOptions:options];
// 將最新寫入相冊的獲取出來惨远,我這里最新寫入的是視頻税产,需要獲取額的也是視頻
PHAsset *phasset = [assetsFetchResults lastObject];
if (phasset) {
if (phasset.mediaType == PHAssetMediaTypeVideo) {
PHImageManager *manager = [PHImageManager defaultManager];
[manager requestAVAssetForVideo:phasset options:nil resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
AVURLAsset *urlAsset = (AVURLAsset *)asset; // 注:這里需要注意薇搁,假如我們沒有進(jìn)行排序宙地,那么在蘋果6上蔓姚,我們這里獲取到的是 nil
NSLog(@"%@",urlAsset.URL);
// 文件保存路徑
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"record.mp4"];
NSData *data = [NSData dataWithContentsOfURL:urlAsset.URL];
NSFileManager *mag =[NSFileManager defaultManager];
// 判斷文件是不是存在
if (![mag fileExistsAtPath:path]) {
[data writeToFile:path atomically:YES];
}else{
[mag removeItemAtPath:path error:nil];
[data writeToFile:path atomically:YES];
}
}];
}
}
重點(diǎn):在 iOS12
之后软驰,必須加上排序的代碼晦鞋,不然在蘋果6上取不到視頻的地址得糜。驗(yàn)證在別的機(jī)型上暫時(shí)沒有問題载绿,針對蘋果6床蜘。