列出iOS中的所有相册

时间:2016-04-19 08:44:42

标签: ios swift

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

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

我发现的所有代码都与图片选择器配合使用,或者他们已经知道了专辑名称。

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

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

我可以列出所有相册吗?

2 个答案:

答案 0 :(得分:9)

简单 列出所有包含图片数量的相册的方式。

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.fetchAssetCollectionsWithType(PHAssetCollectionType.Album, subtype: PHAssetCollectionSubtype.Any, options: options)
    userAlbums.enumerateObjectsUsingBlock{ (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)
    }
  }

答案 1 :(得分:7)

我用它来获得几张专辑,但你可以获得更多:

private func setupPhotos() {
    let fetchOptions = PHFetchOptions()
    let smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, options: fetchOptions)
    let topLevelUserCollections = PHCollectionList.fetchTopLevelUserCollections(with: fetchOptions)
    let allAlbums = [topLevelUserCollections, smartAlbums]

    allAlbums.enumerateObjects {(assetCollection, index, stop) in

        if #available(iOS 9.0, *) {
            fetchOptions.fetchLimit = 1
        }

        let assets = PHAsset.fetchAssets(in: assetCollection, options: fetchOptions)
        if let _ = assets.firstObject {
            let assetObject = MYSpecialAssetContainerStruct(asset: assets)
            self.myDataArray.append(assetObject)
        }
   }
   self.myDataArray.sortInPlace {(a, b) in
       return a.asset.localizedTitle < b.asset.localizedTitle
   }
   tableView.reloadData()
}

编辑:这会获得相册的PHAssetCollections,然后我将它们放入有这种方法的单元格中,以获取相册中的最新图像缩略图。

private func downloadAndSetImage(asset: MYSpecialAssetContainerStruct) {
    guard asset.thumbnail == nil else {
        albumImage.image = asset.thumbnail
        return
    }

    let imageRequestOptions = PHImageRequestOptions()
    imageRequestOptions.isNetworkAccessAllowed = false
    imageRequestOptions.isSynchronous = true
    imageRequestOptions.deliveryMode = .highQualityFormat

    PHImageManager.default().requestImage(
        for: asset.asset,
        targetSize: CGSize(width: 200, height: 200),
        contentMode: .aspectFit,
        options: imageRequestOptions,
        resultHandler: {(img, info) in
            asset.thumbnail = img
            self.albumImage.image = asset.thumbnail
        }
    )
}