2015-06-25 58 views
2

閱讀了幾個教程like this並查看導出視頻的其他代碼後,我們仍然無法解決問題。視頻不總是導出到相機卷:NSFileManager的removeItemAtPath非阻塞?

有時會將新視頻導出到相機膠捲,有時候不會。我們甚至無法一直重現這個問題。

我們可以想象的唯一問題是如果NSFileManager.defaultManager().removeItemAtPath不是阻止調用,但沒有文檔表明它是異步的,所以我們認爲情況並非如此。

每一次,「保存的視頻」 printlnwriteVideoAtPathToSavedPhotosAlbum閉包內被調用,這表明視頻被成功寫入相機膠捲,但我們沒有看到視頻在那裏。

有關如何解決問題的建議?

代碼:

 // -- Get path 
     let fileName = "/editedVideo.mp4" 
     let allPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) 
     let docsPath = allPaths[0] as! NSString 
     let exportPath = docsPath.stringByAppendingFormat(fileName) 
     let exportUrl = NSURL.fileURLWithPath(exportPath as String)! 

     println(exportPath) 

     // -- Remove old video? 
     if NSFileManager.defaultManager().fileExistsAtPath(exportPath as String) { 
      println("Deleting existing file\n") 
      NSFileManager.defaultManager().removeItemAtPath(exportPath as String, error: nil) 
     } 

     // -- Create exporter 
     let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality) 
     exporter.videoComposition = mutableComposition 
     exporter.outputFileType = AVFileTypeMPEG4 
     exporter.outputURL = exportUrl 
     exporter.shouldOptimizeForNetworkUse = true 

     // -- Export video 
     exporter.exportAsynchronouslyWithCompletionHandler({ 
      self.exportDidFinish(exporter) 
     }) 
    } 


    func exportDidFinish(exporter: AVAssetExportSession) { 
     println("Finished exporting video!") 

     // Write out video to photo album 
     let assetLibrary = ALAssetsLibrary() 
     assetLibrary.writeVideoAtPathToSavedPhotosAlbum(exporter.outputURL, completionBlock: {(url: NSURL!, error: NSError!) in 
      println("Saved video \(exporter.outputURL)") 

      if (error != nil) { 
       println("Error saving video") 
      } 
     }) 
    } 

回答

1

一些建議,可以幫助您解決問題:

  1. 檢查影像有效/兼容庫爲完成here

    if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:videoURL]) 
    { 
        [library writeVideoAtPathToSavedPhotosAlbum:videoURL 
               completionBlock:^(NSURL *assetURL, NSError *error){} 
        ]; 
    } 
    
  2. e nsure作爲參考here

    if(error){ 
        NSLog(@"Error: Domain = %@, Code = %@", [error domain], [error localizedDescription]); 
    } else if(assetURL == nil){ 
    
        //It's possible for writing to camera roll to fail, without receiving an error message, but assetURL will be nil 
        //Happens when disk is (almost) full 
        NSLog(@"Error saving to camera roll: no error message, but no url returned"); 
    
    } else { 
        //remove temp file 
        NSError *error; 
        [[NSFileManager defaultManager] removeItemAtURL:fileURL error:&error]; 
        if(error){ 
         NSLog(@"error: %@", [error localizedDescription]); 
        } 
    
    } 
    
  3. 傳回該資產的網址是不是nil考慮使用PHPhotoLibrary,而不是用於導出視頻

    PHPhotoLibrary.sharedPhotoLibrary().performChanges({ 
        let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(videoURL) 
        let assetPlaceholder = assetRequest.placeholderForCreatedAsset 
    }, 
    completionHandler: { success, error in 
        // check and handle error 
        // do something with your asset local identifier 
    }) 
    
+0

嗨,馬克任何機會,你可以用這個問題的幫助? http://stackoverflow.com/questions/34704032/swift-video-records-at-one-size-but-renders-at-wrong-size。謝謝! – Crashalot

+0

我回復了@Crashalot http://stackoverflow.com/a/34794554/51700 –

+0

非常感謝Mark! – Crashalot