簡體   English   中英

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無法保存到保存的相冊中:錯誤Domain = NSURLErrorDomain代碼= -1100“在此服務器上找不到請求的URL。” UserInfo = 0x7f9c13ccb130 {NSUnderlyingError = 0x7f9c13cc3aa0“無法完成操作。沒有這樣的文件或目錄”,

蘋果的文檔中,我認為UIVideoAtPathIsCompatibleWithSavedPhotosAlbum僅適用於本地文件,而不適用於遠程文件。 UISaveVideoAtPathToSavedPhotosAlbum也是如此。

您必須先將文件下載到設備上,然后才能使用UIVideoAtPathIsCompatibleWithSavedPhotosAlbum 我建議使用類似AFNetworking的方法。

使用此代碼,我希望它能正常工作。

    -(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 !");
    });
    }

});
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM