iOS:从url下载视频并保存在图库中……?
iOS: Download video from url and save in gallery......?
我不知道如何从 url 下载视频并将其保存到图库。
BOOL compatible = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum([videoURL path]);
// save
NSLog(@"BOOL compatible....%hhd",compatible);
if (compatible){
UISaveVideoAtPathToSavedPhotosAlbum([videoURL path], nil, nil, nil);
NSLog(@"SAVED!!!! %@",[videoURL path]);
}else
{
NSLog(@"INCOMPATIBLE...");
}
下载视频时显示错误:https://api.quickblox.com/blobs/4185382/download无法保存到已保存的相册:Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo=0x7f9c13ccb130 {NSUnderlyingError=0x7f9c13cc3aa0 "The operation couldn’t be completed. No such file or directory" ,
来自 Apple's documentation 我认为 UIVideoAtPathIsCompatibleWithSavedPhotosAlbum
仅适用于本地文件,不适用于远程文件。 UISaveVideoAtPathToSavedPhotosAlbum
.
也是如此
您需要先将文件下载到您的设备,然后才能使用 UIVideoAtPathIsCompatibleWithSavedPhotosAlbum
。我建议使用 AFNetworking 之类的东西来做到这一点。
使用这个code.I希望它能正常工作。:
-(void)DownloadVideo
{
//download the file in a seperate thread.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"Downloading Started");
NSString *urlToDownload = @"http://www.somewhere.com/thefile.mp4";
NSURL *url = [NSURL URLWithString:urlToDownload];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"thefile.mp4"];
//saving is done on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[urlData writeToFile:filePath atomically:YES];
NSLog(@"File Saved !");
});
}
});
}
我不知道如何从 url 下载视频并将其保存到图库。
BOOL compatible = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum([videoURL path]);
// save
NSLog(@"BOOL compatible....%hhd",compatible);
if (compatible){
UISaveVideoAtPathToSavedPhotosAlbum([videoURL path], nil, nil, nil);
NSLog(@"SAVED!!!! %@",[videoURL path]);
}else
{
NSLog(@"INCOMPATIBLE...");
}
下载视频时显示错误:https://api.quickblox.com/blobs/4185382/download无法保存到已保存的相册:Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo=0x7f9c13ccb130 {NSUnderlyingError=0x7f9c13cc3aa0 "The operation couldn’t be completed. No such file or directory" ,
来自 Apple's documentation 我认为 UIVideoAtPathIsCompatibleWithSavedPhotosAlbum
仅适用于本地文件,不适用于远程文件。 UISaveVideoAtPathToSavedPhotosAlbum
.
您需要先将文件下载到您的设备,然后才能使用 UIVideoAtPathIsCompatibleWithSavedPhotosAlbum
。我建议使用 AFNetworking 之类的东西来做到这一点。
使用这个code.I希望它能正常工作。:
-(void)DownloadVideo
{
//download the file in a seperate thread.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"Downloading Started");
NSString *urlToDownload = @"http://www.somewhere.com/thefile.mp4";
NSURL *url = [NSURL URLWithString:urlToDownload];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"thefile.mp4"];
//saving is done on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[urlData writeToFile:filePath atomically:YES];
NSLog(@"File Saved !");
});
}
});
}