ios - Swift - 如何获取包含 iCloud 库的所有照片的相册?

标签 ios swift

我需要从设备获取所有相册。我使用此代码来获取相册:

func fetchAlbums() {
       // Get all user albums 
        let userAlbumsOptions = PHFetchOptions()
        userAlbumsOptions.predicate = NSPredicate(format: "estimatedAssetCount > 0")


        let userAlbums = PHAssetCollection.fetchAssetCollections(with: PHAssetCollectionType.album, subtype: PHAssetCollectionSubtype.any, options: userAlbumsOptions)

        userAlbums.enumerateObjects( {
            if let collection = $0.0 as? PHAssetCollection {
                print("album title: \(collection.localizedTitle)")

                let onlyImagesOptions = PHFetchOptions()
                onlyImagesOptions.predicate = NSPredicate(format: "mediaType = %i", PHAssetMediaType.image.rawValue)

                if let result = PHAsset.fetchKeyAssets(in: collection, options: onlyImagesOptions) {
                    print("Images count: \(result.count)")
                    if result.count > 0  {
                        //Add album titie
                        self.albumsTitles.append(collection.localizedTitle!)
                        //Add album
                        self.albumsArray.append(result as! PHFetchResult<AnyObject>)
                  }
                }
            }
        } )


    }

但我无法获取所有照片。所有相册不包含 iCloud 库中的照片,只有本地照片。 我使用 Collection View 来预览照片。从所选相册中获取照片的代码示例:

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: AlbumPhotoCollectionViewCell.self), 
                                  for: indexPath as IndexPath) as! AlbumPhotoCollectionViewCell

                    //Array with albums
                    let asset = self.albumsArray[indexSelectedAlbum]

                    let album = asset[indexPath.item]

                    let options = PHImageRequestOptions()

                    options.isNetworkAccessAllowed = true


               PHImageManager.default().requestImage(for: album as! PHAsset , 
                               targetSize: self.assetThumbnailSize,
                               contentMode: .aspectFill,
                               options: options, 
                               resultHandler: {(result, info)in
                                     if result != nil {

                                       cell.albumPhoto.image = result
                              }
                    })


                    return cell
        }

最佳答案

我更改了 fetch 并使其全部正常工作。
旧代码:

if let result = PHAsset.fetchKeyAssets(in: collection, options: onlyImagesOptions){
 //Code
}

新代码

let result = PHAsset.fetchAssets(in: collection, options: onlyImagesOptions)

关于ios - Swift - 如何获取包含 iCloud 库的所有照片的相册?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42996118/

相关文章:

ios - UISearchController 搜索栏动画第一次很慢

ios - Xcode Instruments 的分配工具遗漏了很多

ios - 检查是否使用控制中心

ios - 私有(private) Cocoapod 未在 iOS 模拟器上运行 - 体系结构 x86_64 的 undefined symbol

ios - 如何使 firebase 推送通知工作

ios - 如何在不使用 UIWebView 的情况下在 iOS 中显示动画 svg 文件

ios - 控制 TableViewCell 输出到 TableView 的次数

swift - 如何编写处理 do catch 的模板函数

ios - 为什么当 TableView 在其下方滚动时,UIToolBar 会变成透明/半透明

swift - 无法在 Ubuntu 中安装 Swift