PHFetchRequest returns 零数据与图像的可选数据混合?

PHFetchRequest returns nil data mixed with optional data for images?

我正在学习如何使用 PHFetchRequest 从用户的照片库中获取图像,然后在自定义图像选择器的滚动视图中显示它们但我几乎没有返回数据 并且一些作为可选数据返回,可以展开。

我在 iCloud 上备份了我所有的照片,这可能是我没有收到照片的原因吗??

下面是我的结构中的函数,它获取数据并将其附加到空数组变量...

我做错了什么?谢谢大家!

XCode log showing nil and optional returns

   func getAllImages() {
        let request = PHAsset.fetchAssets(with: .image, options: .none)
        
        DispatchQueue.global(qos: .background).async {
            let options = PHImageRequestOptions()
            options.isSynchronous = true
            
            request.enumerateObjects { (asset, _, _ ) in
                PHCachingImageManager.default().requestImage(for: asset, targetSize: .init(), contentMode: .default, options: options) { (image, _) in
                    print(image?.pngData())

                    // I had to coalesce with an empty UIImage I made as an extension
                    let data1 = Images(image: (image ?? UIImage.emptyImage(with: CGSize(width: 10, height: 10)))!, selected: false)
                    self.data.append(data1)
                }
            }
            
            if request.count == self.data.count {
                self.getGrid()
            }
            
        }
    }

您正在查看 nil 数据,因为图像资源在云端而不是您的 phone。 Optional 是预期的,因为不能保证它存在于您的 phone 上(就像在您的情况下它在云中)。

您可以使用 PHImageRequestOptions.isNetworkAccessAllowed 选项指示获取请求从云端获取照片(如果需要)。默认为 false

A Boolean value that specifies whether Photos can download the requested image from iCloud.

Discussion

If true, and the requested image is not stored on the local device, Photos downloads the image from iCloud. To be notified of the download’s progress, use the progressHandler property to provide a block that Photos calls periodically while downloading the image.

If false (the default), and the image is not on the local device, the PHImageResultIsInCloudKey value in the result handler’s info dictionary indicates that the image is not available unless you enable network access.

progressHandler 注释

您无需实现此功能即可下载 iCloud 照片。 iCloud 照片下载也可以在没有它的情况下使用。 如果您计划实现此功能以显示 UI 上的进度,您应该记住,一次下载会多次触发这些调用。因此,您不能在第一次调用 progressHandler 回调时就认为您的照片下载已完成。

Discussion

If you request an image whose data is not on the local device, and you have enabled downloading with the isNetworkAccessAllowed property, Photos calls your block periodically to report progress and to allow you to cancel the download.