2016-02-09 21 views
0

我已經將.mp4文件從服務器下載到文檔目錄,並調用 writeVideoAtPathToSavedPhotosAlbum方法將其保存到圖庫中,但不能正常工作。資產URL nil.Can有人plz幫助我解決這個問題需要將.MP4文件從文檔文件夾保存到照片庫,但資產url返回無

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"test.mp4"]; 
    NSURL *movieURL = [NSURL fileURLWithPath:path]; 
    ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init]; 
    NSLog(@"Moview URL:%@",movieURL); 
    NSURL *url = [movieURL copy]; 

    [library writeVideoAtPathToSavedPhotosAlbum:url 
           completionBlock:^(NSURL *assetURL, NSError *error) 
    { 
     NSLog(@"Asset Url:%@",assetURL); 
     if(!error) { 
      NSLog(@"\t ! Error"); 
      NSLog(@"\t Error: %@", [error localizedDescription]); 
      NSLog(@"\t Error code %d", [error code]); 
     } 

     if(error != nil) { 
      NSLog(@"\t ERROR != NIL"); 
      NSLog(@"\t Error - Image Failed To Save With Error: %@", [error localizedDescription]); 
      NSLog(@"\t Error code %d", [error code]); 
     } 

     if(error == nil) { 
      NSLog(@"\t ERROR == NIL"); 
     } 
    }]; 
+0

這是排什麼的日誌: - 的NSLog(@ 「Moview網址:%@」, movieURL);?這裏評論它 – Vizllx

+0

這條線的用途是什麼: - NSURL * url = [movieURL copy]; – Vizllx

+0

url是文檔目錄中視頻位置的資產url – leni

回答

1

入住這下面的方法:

-(void)ButtonAction:(id)sender 
    { 
     NSURL *video = [NSURL URLWithString:YOURURL]; 
     NSData *data = [NSData dataWithContentsOfURL:video]; 
     NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
     NSString *filePath = [NSString stringWithFormat:@"%@/myvideo_%@.mp4",docDirPath,CURRENT_TIMESTAMP]; 
     [data writeToFile:filePath atomically:YES]; 

     NSURL *urlPath = [[NSURL alloc] initFileURLWithPath:filePath]; 
     [self saveToCameraRoll:urlPath dict:(NSDictionary *)sender]; 
    } 

    -(void) saveToCameraRoll:(NSURL *)srcURL dict:(NSDictionary *)dictValues 
    { 
     NSLog(@"srcURL: %@", srcURL); 

     ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
     [library writeVideoAtPathToSavedPhotosAlbum:srcURL completionBlock:^(NSURL *assetURL, NSError *error) 
     { 
      NSURL *savedAssetURL = assetURL; 

      NSLog(@"asset url %@",assetURL); 
      if(error) 
      { 
       error handling 
      } 
      else 
      { 
       [library assetForURL:savedAssetURL resultBlock:^(ALAsset * alAsset) 
        { 
          NSLog(@"Saved"); 


        }failureBlock:^(NSError *error) 
        { 
           NSLog(@"Not Saved"); 


        }]; 
      } 
     }]; 
    } 
+0

我仍然收到資產Url爲null.Any的想法,可能是什麼原因。 srcURL返回值,但資產值爲空 – leni

+0

檢查文件路徑後,如果文件路徑存在,則發送到saveTocamerRoll方法。 我已經做到了這一點,它對我來說工作得很好。 –

+0

我試過路徑存在。即使它沒有被保存到相冊和資產url爲空 – leni

相關問題