【问题标题】:Swift - How to fetch videos from camera roll and display them in UICollectionView? [closed]Swift - 如何从相机胶卷中获取视频并将其显示在 UICollectionView 中? [关闭]
【发布时间】:2017-02-14 05:56:34
【问题描述】:

我想在 UICollectionView 中加载所有相机胶卷视频及其持续时间(同样,视频显示在相机胶卷中)。怎么做? 我检查了this 链接。但他们像图片一样展示他们的视频。

【问题讨论】:

标签: ios swift video uicollectionview


【解决方案1】:
// 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 文件中

【讨论】:

  • 你能上传完整的课程吗? @Yogesh Makwana
  • 我没有得到你的代码的第二部分......在哪里写它和函数 getAssetFromPhoto(),我应该在 viewDidLoad() 方法中调用它吗?
  • 是 getAssetFromPhoto() 在 vi​​ewDidLoad() 中调用并用于显示视频缩略图: func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { }
  • 你能上传 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { } 对我来说很难理解:(
  • 我更新了答案,请尝试一下
【解决方案2】:

我推荐使用苹果在 Xcode 中预置的照片框架。这个问题的答案已经在这里填写:

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

苹果自创的照片框架:

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

要添加视频,您可以使用 UIImagePickerCrollerSourceType:

picker.sourceType = UIImagePickerControllerSourceType.Camera

【讨论】:

  • 视频看起来像图像...没有持续时间。
  • 你想要显示视频,而不是图像,对吧?
  • 是的...我只是想加载视频
猜你喜欢
  • 1970-01-01
  • 2015-09-25
  • 1970-01-01
  • 2016-11-08
  • 1970-01-01
  • 2015-06-11
  • 1970-01-01
  • 2018-04-16
  • 1970-01-01
相关资源
最近更新 更多