ios - 列出 iOS 中的所有相册

标签 ios swift

我需要获取 iPhone 上的所有相册。

我知道我可以启动图像选择器让用户从中选择图像。但是我需要开发自己的图片浏览器。

我找到的所有代码都适用于图像选择器,或者他们已经知道相册名称。

我需要先列出所有相册名称然后用户将选择其中一个,然后我将显示该相册中的图像。

所有这些步骤都将在自定义布局中完成。

我可以列出所有相册吗?

最佳答案

简单 列出所有相册和图像计数的方法。

class AlbumModel {
    let name:String
    let count:Int
    let collection:PHAssetCollection
    init(name:String, count:Int, collection:PHAssetCollection) {
      self.name = name
      self.count = count
      self.collection = collection
    }
  }

func listAlbums() {
    var album:[AlbumModel] = [AlbumModel]()

    let options = PHFetchOptions()
    let userAlbums = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: options)
    userAlbums.enumerateObjects{ (object: AnyObject!, count: Int, stop: UnsafeMutablePointer) in
        if object is PHAssetCollection {
            let obj:PHAssetCollection = object as! PHAssetCollection

            let fetchOptions = PHFetchOptions()
            fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
            fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)

            let newAlbum = AlbumModel(name: obj.localizedTitle!, count: obj.estimatedAssetCount, collection:obj)
            album.append(newAlbum)
        }
    }

    for item in album {
        print(item)
    }
}

关于ios - 列出 iOS 中的所有相册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36713164/

相关文章:

ios - 如何创建地址簿和联系人

iOS RxSwift - 如何将 “disconnect” 设为可观察对象?

ios - swift : Closure declaration as like block declaration

ios - 有什么方法可以同步 Map Vector(OpenGL 层)和 UIKit?

ios - UILabel 在 iOS7 中有一个奇怪的灰色顶线/边框,我该如何删除它?

ios - 可以从库的PHAsset中获取图像

ios - iOS应用提交错误:处理json请求时发生错误

swift - CoreData - PickerView 更新 tableView

swift - CMTime除了媒体编解码之外还有什么实际应用吗

ios - 点击日期字段时如何调出日期选择器?