我有一個應用程序,用戶可以在該應用程序中錄製視頻,該視頻保存在照片庫中,並存儲視頻的路徑,以便將來用戶可以再次看到應用內的視頻。問題是,我使用的方法我認爲它給了我一些臨時路徑,幾天後,視頻仍然在畫廊中,但路徑無效,並使應用程序崩潰。這是我使用的代碼:保存視頻並保存到視頻的路徑
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
他們都工作,但幾天後停止工作。
你使用什麼來訪問視頻,或者你從保存的字符串中生成路徑/ URL。 – Idali
我使用存儲在self.videoPath變量中的路徑 – diegomen
那麼,當應用程序重新運行時,重新創建從DB保存的字符串的路徑權利? – Idali