Skip to content

Commit

Permalink
Merge pull request #287 from mikaoj/feature/performance_optimization
Browse files Browse the repository at this point in the history
Performance optimizations
  • Loading branch information
mikaoj authored Sep 26, 2020
2 parents 021e5cc + 974c3fc commit b964825
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
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

0 comments on commit b964825

Please sign in to comment.