繁体   English   中英

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

[英]Swift - How get photo albums with all photos include iCloud Library?

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

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库照片的相册,只有本地照片。 我使用收藏夹视图预览照片。 我的示例代码,用于从选定的相册中获取照片:

    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
        }

我更改抓取方式,一切正常。
旧代码:

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

新密码

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM