Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance optimizations #287

Merged
merged 1 commit into from
Sep 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 19 additions & 22 deletions Sources/Scene/Assets/AssetsCollectionViewDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,25 @@ class AssetsCollectionViewDataSource : NSObject, UICollectionViewDataSource {
private static let videoCellIdentifier = "VideoCell"

var settings: Settings!
var fetchResult: PHFetchResult<PHAsset>
var fetchResult: PHFetchResult<PHAsset> {
didSet {
imageManager.stopCachingImagesForAllAssets()
}
}
var imageSize: CGSize = .zero {
didSet {
imageManager.stopCachingImagesForAllAssets()
}
}

private let imageManager = PHCachingImageManager.default()
private let imageManager = PHCachingImageManager()
private let durationFormatter = DateComponentsFormatter()
private let store: AssetStore

private let scale: CGFloat
private var targetSize: CGSize = .zero
private let contentMode: PHImageContentMode = .aspectFill

init(fetchResult: PHFetchResult<PHAsset>, store: AssetStore, scale: CGFloat = UIScreen.main.scale) {
init(fetchResult: PHFetchResult<PHAsset>, store: AssetStore) {
self.fetchResult = fetchResult
self.store = store
self.scale = scale
durationFormatter.unitsStyle = .positional
durationFormatter.zeroFormattingBehavior = [.pad]
durationFormatter.allowedUnits = [.minute, .second]
Expand Down Expand Up @@ -87,35 +93,26 @@ class AssetsCollectionViewDataSource : NSObject, UICollectionViewDataSource {
collectionView?.register(VideoCollectionViewCell.self, forCellWithReuseIdentifier: videoCellIdentifier)
}

private func loadImage(for asset: PHAsset, in cell: AssetCollectionViewCell?) {
private func loadImage(for asset: PHAsset, in cell: AssetCollectionViewCell) {
// Cancel any pending image requests
if let cell = cell, cell.tag != 0 {
if cell.tag != 0 {
imageManager.cancelImageRequest(PHImageRequestID(cell.tag))
}

// Request image
if let cell = cell {
targetSize = cell.bounds.size.resize(by: scale)
}

cell?.tag = Int(imageManager.requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFill, options: settings.fetch.preview.photoOptions) { (image, _) in
cell.tag = Int(imageManager.requestImage(for: asset, targetSize: imageSize, contentMode: contentMode, options: nil) { (image, _) in
guard let image = image else { return }
cell?.imageView.image = image
cell.imageView.image = image
})
}
}

extension AssetsCollectionViewDataSource: UICollectionViewDataSourcePrefetching {
func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) {
// Touching asset should trigger prefetching
// And prefetch image for that asset
indexPaths.forEach {
let asset = fetchResult[$0.row]
loadImage(for: asset, in: nil)
}
let assets = indexPaths.map { fetchResult[$0.row] }
imageManager.startCachingImages(for: assets, targetSize: imageSize, contentMode: contentMode, options: nil)
}

func collectionView(_ collectionView: UICollectionView, cancelPrefetchingForItemsAt indexPaths: [IndexPath]) {

}
}
3 changes: 3 additions & 0 deletions Sources/Scene/Assets/AssetsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class AssetsViewController: UIViewController {
collectionView.backgroundColor = settings.theme.backgroundColor
collectionView.delegate = self
collectionView.dataSource = dataSource
collectionView.prefetchDataSource = dataSource
AssetsCollectionViewDataSource.registerCellIdentifiersForCollectionView(collectionView)

let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(AssetsViewController.collectionViewLongPressed(_:)))
Expand Down Expand Up @@ -158,6 +159,8 @@ class AssetsViewController: UIViewController {
collectionViewFlowLayout.minimumLineSpacing = itemSpacing
collectionViewFlowLayout.minimumInteritemSpacing = itemSpacing
collectionViewFlowLayout.itemSize = itemSize

dataSource.imageSize = itemSize.resize(by: UIScreen.main.scale)
}

private func updateSelectionIndexForCell(at indexPath: IndexPath) {
Expand Down