首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从摄像机卷中获取视频并在UICollectionView中显示?

如何从摄像机卷中获取视频并在UICollectionView中显示?
EN

Stack Overflow用户
提问于 2017-02-14 05:56:34
回答 2查看 6.8K关注 0票数 3

我想在UICollectionView中加载所有相机滚动视频以及它们的持续时间(同样的方式,视频显示在相机卷中)。如何做到这一点?我查了this链接。但是他们像图像一样展示他们的视频。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-14 07:24:29

代码语言:javascript
复制
// 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文件中
票数 11
EN

Stack Overflow用户

发布于 2017-02-14 06:11:51

我建议使用苹果预装到Xcode中的照片框架。这个问题的答案已经在这里填写:

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

苹果公司创建的照片框架:

Framework/index.html

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

代码语言:javascript
复制
picker.sourceType = UIImagePickerControllerSourceType.Camera
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42218875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档