2016-07-07 116 views
0

我目前正在研究一個小型的swift應用程序,並將一些視頻記錄存儲在應用程序的文檔文件夾中。我想稍後再檢索這些內容。我已經有文件位置的一個這樣的數組:在swift中無法訪問的文檔文件路徑

file:///private/var/mobile/Containers/Data/Application/6C462C4E-05E2-436F-B2E6-F6D9AAAC9361/Documents/videorecords/196F9A75-28C4-4B65-A06B-6111AEF85F01.mov 

現在我想用這樣的文件位置創建一個縮略圖,第一幀和與下面的代碼段連接到我的ImageView:

func createVideoStills() { 
    for video in directoryContents { 
     print("\(video)") 
     do { 
      let asset = AVURLAsset(URL: NSURL(fileURLWithPath: "\(video)"), options: nil) 
      let imgGenerator = AVAssetImageGenerator(asset: asset) 
      imgGenerator.appliesPreferredTrackTransform = true 
      let cgImage = try imgGenerator.copyCGImageAtTime(CMTimeMake(0, 1), actualTime: nil) 
      let uiImage = UIImage(CGImage: cgImage) 
      videoCell.imageView = UIImageView(image: uiImage) 
      //let imageView = UIImageView(image: uiImage) 
     } catch let error as NSError { 
      print("Error generating thumbnail: \(error)") 
     } 
    } 
} 

第一次打印給了我一個如上所述的路徑。但AVURLAsset不喜歡這條路徑,因爲它吐出以下錯誤:

Error generating thumbnail: Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo={NSLocalizedDescription=The requested URL was not found on this server., NSUnderlyingError=0x14ee29170 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

這是奇怪的原因,因爲它是在那裏。任何解決方案如何解決/解決這個問題?

親切的問候,

沃特

回答

1

print("\(video)")的輸出不是文件路徑但文件URL的字符串表示。您需要使用而不是init(fileURLWithPath:)NSURL

看你得到了什麼:

  let asset = AVURLAsset(URL: NSURL(string: video), options: nil) 

(不必要的字符串內插將產生沒有錯誤一些意想不到的結果 - 如 「可選(...)」,所以你應該避免的。)

+0

啊現在對我有意義。我不必要地轉換它。我已經有了NSURL的陣列。所以沒有必要施放它。對我來說太愚蠢了。我知道我正在從URL中創建一個字符串表示。我所要做的只是以下幾點: 'let asset = AVURLAsset(URL:video,options:nil)' 感謝您指點我正確的方向! – Wouter125