2016-07-07 115 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