2015-09-30 111 views
1

我有一個應用程序,用戶可以在該應用程序中錄製視頻,該視頻保存在照片庫中,並存儲視頻的路徑,以便將來用戶可以再次看到應用內的視頻。問題是,我使用的方法我認爲它給了我一些臨時路徑,幾天後,視頻仍然在畫廊中,但路徑無效,並使應用程序崩潰。這是我使用的代碼:保存視頻並保存到視頻的路徑

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { 
    let mediaType: String = info["UIImagePickerControllerMediaType"] as! String 
    if mediaType == "public.movie" { 
     let tempImageURL = info[UIImagePickerControllerMediaURL] as! NSURL! 
     let pathString = tempImageURL.relativeString 
     self.dismissViewControllerAnimated(true, completion: {}) 
     if picker.sourceType == UIImagePickerControllerSourceType.PhotoLibrary { 
      self.videoPath = pathString 
      // Save the path in the DB 
     } else { 
      VideoManager.saveVideo(tempImageURL, onComplete: { (path) -> Void in 
       self.videoPath = path 
       // Save the path in the DB 
      }) 
      var fileManager: NSFileManager = NSFileManager() 
      fileManager.removeItemAtPath(pathString!, error: nil) 
     } 
    } 
} 

而且VideoManager.saveVideo方法的代碼如下:

func saveVideo(videoURL: NSURL, onComplete:((path: String) -> Void)) { 
    var assetsLibrary: ALAssetsLibrary = ALAssetsLibrary() 
    assetsLibrary.writeVideoAtPathToSavedPhotosAlbum(videoURL, completionBlock: { (assetURL: NSURL!, error: NSError!) -> Void in 
     var path: String = error == nil ? "\(assetURL)" : kEmptyString 
     onComplete(path: path) 
    }) 
} 

我不知道我做錯了,我已經試過與方法UISaveVideoAtPathToSavedPhotosAlbum但沒有成功..任何想法?

爲了給多一點信息,當從庫中選擇視頻,給我的網址是類似以下:

file:///private/var/mobile/Containers/Data/Application/D2E8E31B-CEA0-43B0-8EF9-1820F6BDE4A9/tmp/trim.AD855155-AB78-4A16-9AA8-DF2B3F39824E.MOV 

當我使用相機錄製新的視頻,首先我有這樣的URL:

file:///private/var/mobile/Containers/Data/Application/D2E8E31B-CEA0-43B0-8EF9-1820F6BDE4A9/tmp/capture/capturedvideo.MOV 

當我做writeVideoAtPathToSavedPhotosAlbum它返回一個URL,如:

assets-library://asset/asset.MOV?id=958507B5-1353-4DDC-BC07-D9CBC6126657&ext=MOV 

他們都工作,但幾天後停止工作。

+0

你使用什麼來訪問視頻,或者你從保存的字符串中生成路徑/ URL。 – Idali

+0

我使用存儲在self.videoPath變量中的路徑 – diegomen

+0

那麼,當應用程序重新運行時,重新創建從DB保存的字符串的路徑權利? – Idali

回答

1

好了,終於我找到了解決方案,事情是,你不能直接訪問存儲的網址的照片庫,你必須使用assetLibrary.assetForURL方法,我錯過了。到底imagepickercontroller委託方法是這樣的:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { 
    let mediaType: String = info["UIImagePickerControllerMediaType"] as! String 
    if mediaType == "public.movie" { 
     self.dismissViewControllerAnimated(true, completion: {}) 
     if picker.sourceType == UIImagePickerControllerSourceType.PhotoLibrary { 
      let tempImageURL = info[UIImagePickerControllerReferenceURL] as! NSURL! 
      self.videoPath = tempImageURL.absoluteString 
     } else { 
      let tempImageURL = info[UIImagePickerControllerMediaURL] as! NSURL! 
      VideoManager.saveVideo(tempImageURL, onComplete: { (path) -> Void in 
       self.videoPath = path 
      }) 
     } 
    } 
} 

我也失蹤了,當你錄製視頻,你有使用獲得的網址:

let tempImageURL = info[UIImagePickerControllerMediaURL] as! NSURL! 

但是,當你獲得視頻你必須這樣做:

let tempImageURL = info[UIImagePickerControllerReferenceURL] as! NSURL! 

希望它有幫助!

+0

嘿,我想存儲視頻路徑,並且我已經使用UIImagePickerControllerReferenceURL,將來仍然不播放視頻。任何想法? – Max