2016-09-29 53 views
1

我想將本地資源(視頻,照片)分享到Facebook,但Facebook的API要求我必須在本地系統庫中提供一個URL,我如何獲取它?謝謝。如何使用Photos Foundation在iOS系統庫中獲取視頻?

+0

哪種方法是你用來選擇照片或視頻? 'UIImagePickerController'? – Lion

+0

沒有。我使用Photos Foundation獲取圖庫的所有資產,並且我可以獲取每個資產本地標識符。 – Littlecoder

+0

我如何通過提供資產來獲取資產的網址? – Littlecoder

回答

0

第一Didload()這樣的代碼

self.options.predicate = NSPredicate(format: "mediaSubtype == %ld", PHAssetMediaSubtype.videoHighFrameRate.rawValue) 
      self.images = PHAsset.fetchAssets(with: PHAssetMediaType.video, options: nil) 
      self.options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] 

      self.collectionView.reloadData() 
      self.collectionView.dataSource = self 
      self.collectionView.delegate = self 

而且在didselect()這樣的代碼下面

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     let cell = collectionView.cellForItem(at: indexPath as IndexPath) as! custCollectionViewCell 


     var videoURL: NSURL! 

     var resourceArray: [AnyObject] = PHAssetResource.assetResources(for: self.images[indexPath.row]) 
     var paths: [AnyObject] = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as [AnyObject] 

     self.assetResource = resourceArray[0] as! PHAssetResource 


     let filePath = paths[0].appending("/image.mov") 
     videoURL = NSURL.init(fileURLWithPath: filePath) 

     do 
     { 
      try FileManager.default.removeItem(atPath: filePath) 

     } 
     catch 
     { 


     } 
     PHAssetResourceManager.default().writeData(for: self.assetResource, toFile: videoURL as URL, options: nil, completionHandler: {_ in 







     }) 
     let assets = AVAsset(url: NSURL(fileURLWithPath: filePath) as URL) 
     let length = Float(assets.duration.value)/Float(assets.duration.timescale) 
     if length != 0.0 { 




      self.selectedImage = cell.photoImageView.image 
      performSegue(withIdentifier: "show", sender: self) 

     } 

    } 

不過您需要任何幫助,請隨時來問我