2015-10-14 126 views
2

林創建一個簡單的應用程序的視頻數據到錄製的視頻上傳到Web API。在應用程序錄制視頻後,我可以順利播放應用程序內的視頻,但我掙扎訪問視頻進行上傳。檢索從本地文件系統

視頻是assets-library://asset/asset.MOV?id=3AFCEC9B-17DE-4D75-9B87-0AD50BAB9BFF&ext=MOV,可以使用MPMoviePlayerController(contentURL: url),所以我知道它的存在有被加載。

我試過沒有成功以下幾個方法:

方法1

let url = NSURL(fileURLWithPath: thisNote.url!) 
     println("This url = \(thisNote.url)") 
let videoData = NSData(contentsOfURL: url!, options: nil, error: &e) 

錯誤= The operation couldn’t be completed. No such file or directory

方法2

let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) 
let docsDir = dirPaths[0] as! String 
let videoFilePath = docsDir.stringByAppendingPathComponent(Constants.Directories.appFolder) 
let filePath = NSBundle.pathForResource(thisNote.fileName, ofType: "video/quicktime", inDirectory: Constants.Directories.appFolder) 

文件路徑= nil

方法3

var video:NSData = NSData() 
ALAssetsLibrary().assetForURL(url, resultBlock: { (asset : ALAsset!) -> Void in 
    if let rep : ALAssetRepresentation = asset.defaultRepresentation(){ 

     var error: NSError? 
     let length = Int(rep.size()) 
     let from = Int64(0) 
     let data = NSMutableData(length: length)! 
     let numRead = rep.getBytes(UnsafeMutablePointer(data.mutableBytes), fromOffset: from, length: length, error: &error) 

     video = data 

     self.uploadNote(video, note: thisNote) 

    } 
}){ (error : NSError!) -> Void in 
    println("Asset library error: \(error)") 
} 

結果塊未到達,也不是印刷錯誤。

任何幫助,將不勝感激。

回答

1

方法三是最接近最終的解決方案。我最終通過資產庫列舉來找到正確的視頻文件。雖然我確定這不是獲得視頻的最有效方式,但它起作用。這裏是我使用的代碼,我希望它可以幫助別人......

let url = NSURL(string: thisNote.url!) 
    var video:NSData = NSData() 
    AssetLibraryHelper.sharedInstance.assetForURL(url, resultBlock: { (foundAsset : ALAsset!) -> Void in 
     var asset:ALAsset? 

     if foundAsset == nil { 
      // Workaround: Enumerate over the asset library to find matching URL 
      AssetLibraryHelper.sharedInstance.enumerateGroupsWithTypes(
       ALAssetsGroupType(ALAssetsGroupPhotoStream), 
       usingBlock: { (group: ALAssetsGroup?, stop: UnsafeMutablePointer<ObjCBool>) -> Void in 
        if group != nil { 
         group!.enumerateAssetsWithOptions(NSEnumerationOptions.Reverse, usingBlock: {(enumeratedAsset: ALAsset!, index: Int, stopGroup: UnsafeMutablePointer<ObjCBool>) -> Void in 

          if enumeratedAsset != nil { 
            asset = enumeratedAsset 
            stop.initialize(true) 
            stopGroup.initialize(true) 
          } 

         }) 
        } 
       }, failureBlock: { (error: NSError!) -> Void in 
       print("Error enumerating assets \(error)") 
      }) 
     } 
     else { 
      asset = foundAsset 
     } 

     print("Asset is \(asset)") 

     if let rep : ALAssetRepresentation = asset!.defaultRepresentation(){ 

      let image = UIImage(CGImage: rep.fullResolutionImage().takeUnretainedValue()) 
      let imageData = UIImageJPEGRepresentation(image, 0.7) 

      var error: NSError? 
      let length = Int(rep.size()) 
      let from = Int64(0) 
      let data = NSMutableData(length: length)! 
      let _ = rep.getBytes(UnsafeMutablePointer(data.mutableBytes), fromOffset: from, length: length, error: &error) 

      video = data 

      self.uploadNote(video, note: thisNote, imageFile: imageData!) 

     } 
     }){ (error : NSError!) -> Void in 
      print("Asset library error: \(error)") 
     } 
+0

什麼是AssetLibraryHelper也是一樣的? –