2017-02-14 94 views
0

我想加載UICollectionView中的所有相機膠捲視頻及其持續時間(同樣的方式,視頻以相機膠捲顯示)。如何做? 我檢查了this鏈接。但他們展示了他們的視頻,如圖像。Swift - 如何從相機膠捲獲取視頻並將其顯示在UICollectionView中?

+0

的可能的複製:([SWIFT如何加載從照片庫中的照片,而無需使用的UIImagePickerController?] http://stackoverflow.com/questions/35115117/swift-how-to-load-photos-from-photo-library-without-using-uiimagepickercontroll) –

回答

3
  // get all videos from Photos library you need to import Photos framework 
      var photos: PHFetchResult<PHAsset>! // user photos array in collectionView for disaplying video thumnail 
      func getAssetFromPhoto() { 
       let options = PHFetchOptions() 
       options.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: true) ] 
       options.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.video.rawValue) 
       photos = PHAsset.fetchAssets(with: options) 
       print(photos) 
       photoCollectionView.reloadData() // reload your collectionView 
      } 

     // For displaying thumnait image and video dutation 
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoCell", for: indexPath) as! PhotoCollectionViewCell 
     let asset = photos!.object(at: indexPath.row) 
     let width: CGFloat = 150 
     let height: CGFloat = 150 
     let size = CGSize(width:width, height:height) 
     PHImageManager.default().requestImage(for: asset, targetSize: size, contentMode: PHImageContentMode.aspectFill, options: nil) { (image, userInfo) -> Void in 

        self.photoImageView.image = image 
        self.lblVideoTime.text = String(format: "%02d:%02d",Int((asset.duration/60)),Int(asset.duration) % 60) 

       } 
     return cell 
    } 
  • 注:隱私 - 圖片庫使用情況說明 - >我需要你的照片庫中添加此項到Info.plist文件
+0

你能上傳全班嗎? @Yogesh Makwana –

+0

我沒有得到你的代碼的第二部分...在哪裏寫和函數** getAssetFromPhoto()**,我應該在viewDidLoad()方法中調用它? –

+0

yes getAssetFromPhoto()調用viewDidLoad()並顯示視頻thumnail:func collectionView(_ collectionView:UICollectionView,cellForItemAt indexPath:IndexPath) - > UICollectionViewCell { } –

0

我推薦使用蘋果已經預置到Xcode中的照片框架。在這個問題的答案已經被填寫在這裏:

https://developer.apple.com/library/prerelease/ios//documentation/Photos/Reference/Photos_Framework/index.html

要添加視頻,你可以使用一個UIImagePickerCrollerSourceType:

picker.sourceType = UIImagePickerControllerSourceType.Camera 

swift: How to load photos from photo library without using UIImagePickerController?

的照片框架蘋果公司創建

+0

視頻看起來像圖像......那裏沒有持續時間。 –

+0

你想顯示視頻,而不是圖像,對不對? –

+0

是的...我只是想加載視頻 –

相關問題