2017-02-14 46 views
0

UICollectionViewですべてのカメラロール動画を再生したい(同じ方法で、動画はカメラロールに表示されます)。それを行う方法? thisリンクを確認しました。しかし彼らは映像のような映像を見せた。Swift - カメラロールからビデオを取得してUICollectionViewに表示する方法は?

+0

の可能性のある重複:([迅速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

私はあなたのコードの2番目の部分を取得していない...どこでそれを書くために関数** getAssetFromPhoto()**、私はviewDidLoad()メソッドで呼び出す必要がありますか? –

+0

のviewDidLoadでYES getAssetFromPhoto()の呼び出し()およびビデオthumnailを表示する:collectionView(:UICollectionView、cellForItemAt indexPath:_ collectionView IndexPath)funcを - > UICollectionViewCell { } –

0

リンゴがXcodeに組み込まれている写真フレームワークを使用することをお勧めします。この質問への答えはすでにここに記入されています

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

アップルによって作成された写真のフレームワーク:あなたはUIImagePickerCrollerSourceTypeを使用することができ、ビデオを追加するには

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

picker.sourceType = UIImagePickerControllerSourceType.Camera 
+0

ビデオはイメージのようです...そこには時間がありません。 –

+0

画像ではなく、動画を表示したいですか? –

+0

はい...私はちょうどビデオをロードしたい –

関連する問題