ios - 如何在swift中使用 Collection View 显示文件夹中的图像

标签 ios swift

我正在使用 UICollectionView 获取图库图像并显示在 UICollectionViewCell 中,但我需要在一个文件夹中显示所有图像。

有什么解决办法吗?

最佳答案

你可以试试这个方法。下面的代码是从图库中获取“Repost”文件夹并获取其中的所有照片。

 func getImages() {

    image = []
    let albumName = "Repost" //album name
    let fetchOptions = PHFetchOptions()

    var assetCollection = PHAssetCollection()
    fetchOptions.predicate = NSPredicate(format: "title = %@", albumName)
    let collection:PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)

    if let _:AnyObject = collection.firstObject{
        //found the album
        assetCollection = collection.firstObject!
        albumFound = true
    }
    else { albumFound = false
        print("album not found")
    }

    let assets = PHAsset.fetchAssets(in: assetCollection, options: nil) as! PHFetchResult<AnyObject>

    assets.enumerateObjects({ (object, count, stop) in
        // self.cameraAssets.add(object)
        if object.mediaType == .image
        {
            self.image.append(object as! PHAsset) // for image
        }
    })

    self.image.reverse()
    self.imgcollection.reloadData()
}

不要忘记导入照片框架。 谢谢。

关于ios - 如何在swift中使用 Collection View 显示文件夹中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53295169/

相关文章:

ios - 具有 iOS 自动布局约束的 UIScrollView : Wrong size for subviews

ios - 两个日期之间经过的天数总是相差一天

ios - 具有 UIWebView 或自定义 View Controller 的 iPad 应用程序

iOS 启动图像在 Dev 中有效,但在 App Store 版本中无效

swift - 滚动tableview重置水平 ScrollView 页面控件

swift - 如何让我的 Swift 单元测试访问 Parse 的 SDK?

iphone - 更改引脚设计

ios - 如何使用 UIApplication 和 openURL 并从 foo ://q=string? 调用 "string"上的 swift 函数

ios - UI图像大小与实际图像分辨率不同(UIImageWriteToSavedPhotosAlbum)

ios - 如何在与默认目标中的数据不同的数据/应用程序(SQLite db、UserDefaults)上运行 Xcode 测试?