gpt4 book ai didi

ios - 如何将视频下载到 iPhone 照片库?

转载 作者:行者123 更新时间:2023-11-28 18:32:47 25 4
gpt4 key购买 nike

我正在尝试将视频从 URL 下载到我的 iPhone 设备的相册。我做了以下代码。它适用于我的模拟器,但不适用于实际的 iOS 设备。我在 iPhone 和 iPad mini 上都试过了。

这是我的代码

//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;
}

最佳答案

// 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 :)");
}

}];

注意:不要忘记导入 #import <AssetsLibrary/AssetsLibrary.h>

关于ios - 如何将视频下载到 iPhone 照片库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24993924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com