照片资产迭代

问题描述

我正在处理一些照片资产,我正在尝试查看我的相机胶卷并打印出每张照片中的所有资产。我知道它需要是一个 for 循环,但不完全确定如何编写它

guard let selectedindexPath = collectionView.indexPathsForSelectedItems?.first else { return nil }

let sectionType = sections[selectedindexPath.section]
let item = selectedindexPath.item
let assets: PHFetchResult<PHAsset>
let title: String

switch sectionType {
case .all:
  assets = allPhotos
  title = AlbumCollectionSectionType.all.description

case .smartAlbums,.userCollections:
  let album =
    sectionType == .smartAlbums ? smartAlbums[item] : userCollections[item]
  assets = PHAsset.fetchAssets(in: album,options: nil)
  title = album.localizedTitle ?? ""
}
print("\(assets)")
print("\(assets.object(at: 0))")
return PhotosCollectionViewController(assets: assets,title: title,coder: coder)'

解决方法

您可以在 .enumerateObjects 上使用 assets 循环遍历它们:

assets.enumerateObjects { (asset,count,stop) in
    //asset is the PHAsset
}