2016-09-29 59 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) 

     } 

    } 

不过您需要任何帮助,请随时来问我