2017-03-24 1 views
0

기록 된 비디오를 Post 오브젝트와 함께 문서 디렉토리에 저장하고 Post 오브젝트의 콜렉션보기를 채우는 앱이 있습니다. 그러나 응용 프로그램을 다시 시작하면 컬렉션보기가 비어 있으므로 docs 디렉토리에 저장되는 비디오는 계속 유지되지 않습니다 (적어도 문제라고 생각합니다).iOS 문서 디렉토리의 데이터 유지하기

  //Add thumbnail & video path to Post object 
      if let videoImage = try? assetImageGenerate.copyCGImage(at: time, actualTime: nil) { 
       let video = Post(pathToVideo: URL(fileURLWithPath: docDataPath), thumbnail: UIImage(cgImage: videoImage)) 
       posts.append(video) 

그래서 내가 그에게 경로를 제공 할 : 비디오 경로 및 썸네일 내 객체에 추가되는 곳이며, 특히

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 

    let mediaType = info[UIImagePickerControllerMediaType] as! NSString 
    dismiss(animated: true, completion: nil) 
    if mediaType == kUTTypeMovie { 
     var uniqueVideoID = "" 
     var videoURL:NSURL? = NSURL() 
     var uniqueID = "" 

     uniqueID = NSUUID().uuidString 

     // Get the path as URL and store the data in myVideoVarData 
     videoURL = info[UIImagePickerControllerMediaURL] as? URL as NSURL? 
     let myVideoVarData = try! Data(contentsOf: videoURL! as URL) 

     // Write data to temp diroctory 
     let tempPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true) 
     let tempDocumentsDirectory: AnyObject = tempPath[0] as AnyObject 
     uniqueVideoID = uniqueID + "TEMPVIDEO.MOV" 
     let tempDataPath = tempDocumentsDirectory.appendingPathComponent(uniqueVideoID) as String 
     try? myVideoVarData.write(to: URL(fileURLWithPath: tempDataPath), options: []) 

     // Get the time value of the video 
     let fileURL = URL(fileURLWithPath: tempDataPath) 
     let asset = AVAsset(url: fileURL) 
     let duration : CMTime = asset.duration 

     // Remove the data from the temp Document Diroctory. 
     do{ 
      let fileManager = FileManager.default 
      try fileManager.removeItem(atPath: tempDataPath) 
     } catch { 
      //Do nothing 
     } 

     // Check to see if video is under the 18500 (:30 seconds) 
     if duration.value <= 18500 { 

      // Write the data to the Document Directory 
      let docPaths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true) 
      let documentsDirectory: AnyObject = docPaths[0] as AnyObject 
      uniqueVideoID = uniqueID + "VIDEO.MOV" 
      let docDataPath = documentsDirectory.appendingPathComponent(uniqueVideoID) as String 
      try? myVideoVarData.write(to: URL(fileURLWithPath: docDataPath), options: []) 
      print("docDataPath under picker ",docDataPath) 
      print("Video saved to documents directory") 

      //Create a thumbnail image from the video 
      let assetImageGenerate = AVAssetImageGenerator(asset: asset) 
      assetImageGenerate.appliesPreferredTrackTransform = true 
      let time = CMTimeMake(asset.duration.value/3, asset.duration.timescale) 
      if let videoImage = try? assetImageGenerate.copyCGImage(at: time, actualTime: nil) { 
       //Add thumbnail & video path to Post object 
       let video = Post(pathToVideo: URL(fileURLWithPath: docDataPath), thumbnail: UIImage(cgImage: videoImage)) 
       posts.append(video) 
       print("Video saved to Post object") 
      } 
     }else{ 
      print("Video not saved") 
     } 
    } 
} 

:

비디오를 저장하는 기능입니다 문서 디렉토리의 비디오에; 데이터가 거기에 계속 있도록하려면 어떻게해야합니까?

편집 :

enter image description here

답변

0

은 비디오 장치에 저장되고 있는지 확인하려면, 엑스 코드에 장치를 연결하고 엑스 코드> 장치를 도우로 이동합니다. 그런 다음 왼쪽의 기기를 선택하고 설치된 앱 목록에서 앱을 찾습니다. 앱을 선택하고 목록 하단의 톱니 바퀴 아이콘을 클릭하고 '컨테이너 표시'를 누릅니다. 잠시 기다리면 앱의 모든 폴더가 표시됩니다.

둘째, 비디오를 쓰고 삭제하고 다시 쓰는 이유와 'try?' 실제로 파일을 쓰는 동안 던져진 예외를 잡는 대신에?

+0

동영상이 확실히 저장되는 것처럼 보입니다. 편집 한 스크린 샷을 게시했습니다. – KingTim

+0

그래서 다음 줄이 예외를 던질 수 있다고 생각합니다. '시도 하시겠습니까? assetImageGenerate.copyCGImage' – Garfield81

+0

또한 디버거에서 코드를 단계별로 실행 해 보셨습니까? – Garfield81