How do I replace defaultImageManager with PHCacheImageManager

100 views Asked by At

I am trying to take this Apple Sample Project and use PHCacheImageManager instead of the default PHImageManager.

As it stands I am not sure where to initially cache the images or where to remove the images that are no longer needed in the cache as the user scrolls through the UICollectionView. After setting the imageManager variable to a PHCacheImageManager I pull the intial set of assets like so

    PHPhotoLibrary.requestAuthorization { (status: PHAuthorizationStatus) in
        if status == .authorized {
            let fetchOptions = PHFetchOptions()
            fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
            self.fetchResults = PHAsset.fetchAssets(with: .image, options: fetchOptions)
            self.fetchResults?.enumerateObjects({asset, index, stop in
                self.assets.append(asset)
            })

            DispatchQueue.main.async {
                // Reload collection view once we've determined our Photos permissions.
                print("assets count: \(self.assets.count)")
                self.collectionView.reloadData()
            }
        } else {
            self.displayPhotoAccessDeniedAlert()
        }
    }

but I am not sure how to modify functions which will have a functionality similar to this given there is no set thumbnail size and the added and removed assets are dependent upon a users scroll. Would it be correct to place the code below in willDisplayCell and didEndDisplayingCell

imageManager.stopCachingImages(for: removedAssets,
                               targetSize: thumbnailSize, contentMode: .aspectFill, options: nil)
imageManager.startCachingImages(for: addedAssets,
                                   targetSize: thumbnailSize, contentMode: .aspectFill, options: nil)
0

There are 0 answers