目前知道的三種方法可以保存視頻至本地相冊:
方法一:iOS3 之后一直可以使用五嫂,兼容性極強;
方法二:支持 iOS9 以前村斟,在 iOS9 之后被棄用贫导;
方法二:在 iOS8 之后可以使用,一直沿用至今蟆盹。
1孩灯、UIKIT_EXTERN 全局方法
BOOL videoCompatible = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(item.savePath);
//檢查視頻能否保存至相冊
if (videoCompatible) {
UISaveVideoAtPathToSavedPhotosAlbum(item.savePath, self,
@selector(video:didFinishSavingWithError:contextInfo:), nil);
} else {
NSLog(@"該視頻無法保存至相冊");
}
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (error) {
NSLog(@"保存視頻失敗:%@", error);
} else {
NSLog(@"保存視頻成功");
}
}
2逾滥、ALAssetsLibrary(iOS 9 廢棄)
//#import <AssetsLibrary/AssetsLibrary.h>
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:item.savePath]
completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
NSLog(@"Save video fail:%@",error);
} else {
NSLog(@"Save video succeed.");
}
}];
3峰档、PHPhotoLibrary(iOS 8 支持)
//#import <Photos/Photos.h>
PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];
[photoLibrary performChanges:^{
[PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL
fileURLWithPath:item.savePath]];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(@"已將視頻保存至相冊");
} else {
NSLog(@"未能保存視頻到相冊");
}
}];