0
我正在處理一個20秒的視頻。我正要將該視頻保存在我的照片庫中。成功修剪視頻後,我需要調用這個函數,任何人都可以幫助我在Swift中轉換下面的代碼。我粘貼了兩個版本的代碼,如果有人能識別我的錯誤?使用Swift將視頻文件保存到照片庫
目標C
-(void)writeVideoToPhotoLibrary:(NSURL*)aURL
{
NSURL *url = aURL;
NSData *data = [NSData dataWithContentsOfURL:url];
// Write it to cache directory
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"file.mov"];
[data writeToFile:path atomically:YES];
NSLog(@"Path:%@",path);
// After that use this path to save it to PhotoLibrary
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:path] completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
NSLog(@"%@", error.description);
}else {
NSLog(@"Done :)");
}
}];
}
夫特
func SaveVideoToPhotoLibrary(outputFileURL: NSURL)
{
assetsLibrary = ALAssetsLibrary()
let videoURL = outputFileURL as NSURL?
if let library = assetsLibrary{
if let url = videoURL{
library.writeVideoAtPathToSavedPhotosAlbum(url,
completionBlock: {(url: NSURL!, error: NSError!) in
print(url)
if let theError = error{
print("Error happened while saving the video")
print("The error is = \(theError)")
} else {
print("no errors happened")
}
})
} else {
print("Could not find the video in the app bundle")
}
}
}