简体   繁体   English

如何显示库中的实时照片视频并在iOS Swift的收藏夹视图中显示?

[英]How to display Live-photos Videos from library and display in Collection-View in iOS Swift?

For Displaying the Live-Photos: 显示实况照片:

func fetchLivePhotos() {

    let options = PHFetchOptions()

    options.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: false) ]

    options.predicate = NSPredicate(format: "(mediaSubtype & %d) != 0",PHAssetMediaSubtype.photoLive.rawValue)

    DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
        self.livePhotoAssets = PHAsset.fetchAssets(with: options)

        DispatchQueue.main.async {
            self.collectionView?.reloadData()
        }
    }
}

And For Displaying in Grid Code is below: 而在网格代码中显示如下:

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MediaCell", for: indexPath) as! MediaCell

        if let asset = livePhotoAssets?[indexPath.row]{
            let options = PHImageRequestOptions()
            options.isNetworkAccessAllowed = true

            let targetSize = CGSize(width: 200, height: 200)
            PHImageManager.default().requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFill, options: options, resultHandler: { (image: UIImage?, info: [AnyHashable : Any]?) in
                cell.backgroundImageView.image = image
            })
        }

        cell.selectedImage.isHidden = true
        return cell
    }

And For Fetching Video, I am using this predicate: 对于获取视频,我使用以下谓词:

  option.predicate = NSPredicate(format: "mediaType == %i",PHAssetMediaType.video.rawValue)

But in Collectionview both are not display. 但是在Collectionview中都不会显示。

try to override the method from UICollectionViewDataSource protocol: 尝试覆盖UICollectionViewDataSource协议中的方法:

public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if let count = livePhotoAssets?.count {
        return count
    }
    return 0
}

also: 也:

don't forget to set the ViewController as table view datasource. 不要忘记将ViewController设置为表视图数据源。 don't forget to attach collectionView property in storyboard, if you are create it from storyboard. 如果您是从情节提要中创建的,请不要忘记在情节提要中附加collectionView属性。

i have build it and works perfectly. 我已经建立并完美地工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在集合视图 swift 5 中更改动态行的颜色 - How to change color of dynamic row in collection-view swift 5 Swift-如何从相册加载视频并将其显示在收藏夹视图中 - Swift - How to load videos from photo album and display them in collection view 如何在文档中显示所有视频以及相机相册中的照片? - How to display all the videos along with photos from camera album in documents? 如何在控制中心(swift iOS)中显示“实时” - how to display “live” in the control center (swift iOS) 如何分别获取基于相机的图像和基于媒体的图像/视频以在iOS设备的收藏夹视图中显示 - How to fetch camera based and media based images/videos separately to display in collection view in iOS device 如何从JSON响应中获取图像并在Collection View iOS中显示 - How to get image from json response and display in collection view ios 如何在集合视图iOS中显示资产URL - How to Display asset url In collection view ios 如何从照片库中检索数组中的所有照片和视频? - How to retrieve all the photos and videos in an array from Photos library? 如何使用Swift在IOS中的应用商店中使用自定义标头操作在Tableview中显示Collection View - How to Display Collection View inside tableview with custom header action like app store in IOS using Swift 如何实现小视图变成全屏视图的效果? 类似于 Snapchat 从集合视图到内容视图的转换 - How to achieve effect where small view becomes full screen view? Similar to Snapchat transition from collection-view to content view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM