Swift - 如何让所有照片的相册都包含 iCloud Library?
Posted
技术标签:
【中文标题】Swift - 如何让所有照片的相册都包含 iCloud Library?【英文标题】:Swift - How get photo albums with all photos include iCloud Library? 【发布时间】:2017-03-24 09:47:13 【问题描述】:我需要从设备中获取所有相册。我使用此代码获取专辑:
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
【问题讨论】:
【参考方案1】:我改变了 fetch 和所有工作。 旧代码:
if let result = PHAsset.fetchKeyAssets(in: collection, options: onlyImagesOptions)
//Code
新代码
let result = PHAsset.fetchAssets(in: collection, options: onlyImagesOptions)
【讨论】:
以上是关于Swift - 如何让所有照片的相册都包含 iCloud Library?的主要内容,如果未能解决你的问题,请参考以下文章