【问题标题】:How to load images and videos in same UICollectionView in iOS如何在 iOS 的同一个 UICollectionView 中加载图像和视频
【发布时间】:2017-02-26 11:29:16
【问题描述】:

我正在尝试创建一个集合视图,它可以根据时间戳显示相机胶卷中的所有图像和视频。

我只能加载图像或视频,不能同时加载。

我试过下面的代码

        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
        }

正如您在上面的代码片段中看到的那样,在定义选项时,我们只能要求视频收集或图像收集。

我怎样才能将它们结合起来。 请注意:我愿意接受任何类型的开发建议,作为一种解决方法,我将在一个集合中结合 PHFetchResult 并填充我的集合视图。

这是唯一的方法吗?或者 Apple 是否提供了相同的 api。

【问题讨论】:

    标签: ios swift avfoundation photokit


    【解决方案1】:

    您是否尝试过创建 OR 谓词?

    let videoPredicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.video.rawValue)
    let imagePredicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)
    let predicate = NSCompoundPredicate(orPredicateWithSubPredicates: [videoPredicate, imagePredicate])
    
    options.predicate = predicate
    

    甚至根本不提供谓词。谓词用于过滤结果。如果您不想过滤结果,请不要使用谓词。

    【讨论】:

    • 我正在尝试您的解决方案,但我没有想到!将支持投票
    • 您可能需要一个 OR 谓词,因为 (type == Image AND type == Movie) 保证是空集,即零项。试试:NSCompoundPredicate(orPredicateWithSubpredicates: [videoPredicate, imagePredicate]).
    猜你喜欢
    • 1970-01-01
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 2015-03-31
    • 1970-01-01
    相关资源
    最近更新 更多