Ios 如何将视频下载到iPhone照片库?
我正在尝试从iPhone设备相册的URL下载视频。我做了以下代码。它可以与我的模拟器一起工作,但不能与实际的iOS设备一起工作。我用iPhone和iPad mini都试过了 这是我的密码Ios 如何将视频下载到iPhone照片库?,ios,objective-c,Ios,Objective C,我正在尝试从iPhone设备相册的URL下载视频。我做了以下代码。它可以与我的模拟器一起工作,但不能与实际的iOS设备一起工作。我用iPhone和iPad mini都试过了 这是我的密码 //Download method - (IBAction)methodToDownload:(id)sender { ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; UISaveVideoAtPathToSavedPho
//Download method
- (IBAction)methodToDownload:(id)sender
{
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
UISaveVideoAtPathToSavedPhotosAlbum([self saveVideoToLocal], self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
library = nil;
}
- (void)video:(NSString *) videoPath didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
if(error)
NSLog(@"didFinishSavingWithError: %@", error);
else
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Download" message:@"Download completed." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alertView show];
//Delete File after creation
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
BOOL success = [fileManager removeItemAtPath:videoPath error:&error];
if (success) {
// UIAlertView *removeSuccessFulAlert=[[UIAlertView alloc]initWithTitle:@"Congratulation:" message:@"Successfully removed" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
// [removeSuccessFulAlert show];
NSLog(@"Remove Completed");
}
else
{
NSLog(@"Could not delete file -:%@ ",[error localizedDescription]);
}
}
}
//Save file to locally
- (NSString *)saveVideoToLocal {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/test.%@", documentsDirectory,@"mp4"];
NSLog(@"filePath %@",filePath);
//download the file in a seperate thread.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"Downloading Started");
NSURL *url = [NSURL URLWithString:@"XYZ_MOVIE_URL.mov"];
NSLog(@"URL is %@",url);
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
//saving is done on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[urlData writeToFile:filePath atomically:YES];
});
}
});
return filePath;
}
注意:不要忘记导入#导入
注意:别忘了导入
\import
你说的“它不工作”是什么意思?你有错误吗?没有。但我的相册中没有任何视频。你尝试过跟踪吗?正在执行哪个If/else?是,正在尝试获取解决方案。在询问“为什么此代码不工作?”之前,请始终记住跟踪您的代码。根据S.O.的规定,这样的问题被认为是离题的。你说的“它不起作用”是什么意思?你有错误吗?没有。但我的相册中没有任何视频。你尝试过跟踪吗?正在执行哪个If/else?是,正在尝试获取解决方案。在询问“为什么此代码不工作?”之前,请始终记住跟踪您的代码。根据S.O.的规定,此类问题被视为离题。
// I am using simple way to download the video, You can use according to you.
NSURL *url = [NSURL URLWithString:@"yourfileURL"];
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];
// 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 :)");
}
}];