如何将视频剪辑保存到照片库-ios 8&;9
我努力将视频剪辑保存到照片库。 这里的代码如下,但它不工作。 虽然saveVideo返回true,但我打开照片库无法获取视频剪辑 文件路径 是本地文档目录上的位置如何将视频剪辑保存到照片库-ios 8&;9,ios,save,ios9,image-gallery,Ios,Save,Ios9,Image Gallery,我努力将视频剪辑保存到照片库。 这里的代码如下,但它不工作。 虽然saveVideo返回true,但我打开照片库无法获取视频剪辑 文件路径 是本地文档目录上的位置 func saveVideo(filePath: String) { UISaveVideoAtPathToSavedPhotosAlbum (filePath, self,"video:didFinishSavingWithError:contextInfo:", nil) } func video(filePath: Str
func saveVideo(filePath: String) {
UISaveVideoAtPathToSavedPhotosAlbum (filePath, self,"video:didFinishSavingWithError:contextInfo:", nil)
}
func video(filePath: String, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
if error == nil {
let ac = UIAlertController(title: "Saved!", message: "Your altered video has been saved to your gallery.", preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(ac, animated: true, completion: nil)
} else {
let ac = UIAlertController(title: "Save error", message: error?.localizedDescription, preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(ac, animated: true, completion: nil)
}
}
func保存视频(文件路径:字符串){
UISaveVideoAtPathToSavedPhotosAlbum(文件路径,自我,“视频:didFinishSavingWithError:contextInfo:”,无)
}
func视频(文件路径:字符串,DidFinishSavingWither错误:NSError?,contextInfo:UnsafePointer){
如果错误==nil{
让ac=UIAlertController(标题:“已保存!”,消息:“您更改的视频已保存到您的多媒体资料中。”,首选样式:。警报)
ac.addAction(UIAlertAction(标题:“确定”,样式:。默认,处理程序:nil))
self.presentViewController(ac,动画:true,完成:nil)
}否则{
让ac=UIAlertController(标题:“保存错误”,消息:错误?.localizedDescription,首选样式:。警报)
ac.addAction(UIAlertAction(标题:“确定”,样式:。默认,处理程序:nil))
self.presentViewController(ac,动画:true,完成:nil)
}
}
试试这个:-
对于目标-C.
此代码用于从internet下载视频并将其保存到照片库
NSURL *videoUrl = [NSURL URLWithString:[NSString stringWithFormat:@"Your Video Url"]];
dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(q, ^{
NSData *videoData = [NSData dataWithContentsOfURL:videoUrl];
dispatch_async(dispatch_get_main_queue(), ^{
// Write it to cache directory
NSString *videoPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"file.mov"];
[videoData writeToFile:videoPath atomically:YES];
// After that use this path to save it to PhotoLibrary
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:videoPath] completionBlock:^(NSURL *assetURL, NSError *error)
{
if (error)
{
NSLog("Error");
}
else
{
NSLog("Success");
}
}];
});
});
在为iOS 9.0或更高版本开发应用程序时,您可以使用
PHPhotoLibrary
代替ALAssetsLibrary
通过转换成Swift语法,您可以在Swift中使用上述代码。试试这个:-
对于目标-C.
此代码用于从internet下载视频并将其保存到照片库
NSURL *videoUrl = [NSURL URLWithString:[NSString stringWithFormat:@"Your Video Url"]];
dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(q, ^{
NSData *videoData = [NSData dataWithContentsOfURL:videoUrl];
dispatch_async(dispatch_get_main_queue(), ^{
// Write it to cache directory
NSString *videoPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"file.mov"];
[videoData writeToFile:videoPath atomically:YES];
// After that use this path to save it to PhotoLibrary
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:videoPath] completionBlock:^(NSURL *assetURL, NSError *error)
{
if (error)
{
NSLog("Error");
}
else
{
NSLog("Success");
}
}];
});
});
在为iOS 9.0或更高版本开发应用程序时,您可以使用
PHPhotoLibrary
代替ALAssetsLibrary
通过转换成Swift语法,您可以在Swift中使用上述代码。尝试使用此类别来存储和更新相位集(图像/视频)
在此库中,您必须将PHAsset对象提供到块中,以便进行类别操作。尝试使用此类别来存储和更新PHAsset(图像/视频)
PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in
let createAssetRequest: PHAssetChangeRequest = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(NSURL(string: filePath)!)!
createAssetRequest.placeholderForCreatedAsset
}) { (success, error) -> Void in
if success {
//popup alert success
}
else {
//popup alert unsuccess
}
}
在此库中,您必须将PHAsset对象提供到块中,用于分类操作。AlasSetLibrary将在iOS 9.0中被弃用。好的,我将其转换为swift和iOS 9,然后更新我的答案。好吗?您可以使用
PHPhotoLibrary
代替ALAssetsLibrary
是的,我使用PHPhotoLibrary得到了解决方案。Thanks@user831098你能给我那个代码吗?或者用那个代码编辑我的答案。我会在iOS 9.0中弃用这个库。好的,我会把它转换成swift和iOS 9,然后更新我的答案。好吗?您可以使用PHPhotoLibrary
代替ALAssetsLibrary
是的,我使用PHPhotoLibrary得到了解决方案。Thanks@user831098你能给我那个代码或者用那个代码编辑我的答案吗。用桥头文件转换这个库。用桥头文件转换这个库。
PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in
let createAssetRequest: PHAssetChangeRequest = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(NSURL(string: filePath)!)!
createAssetRequest.placeholderForCreatedAsset
}) { (success, error) -> Void in
if success {
//popup alert success
}
else {
//popup alert unsuccess
}
}