Files
GitHawk/Classes/Systems/ImageCellHeightCache.swift
Ryan Nystrom 4adf511ea4 Comment cell images don't scale to fit (#903)
* images scale to top

* layout image view based on image size
2017-11-09 10:37:12 -05:00

45 lines
1.4 KiB
Swift

//
// ImageCellHeightCache.swift
// Freetime
//
// Created by Ryan Nystrom on 7/5/17.
// Copyright © 2017 Ryan Nystrom. All rights reserved.
//
import Foundation
import IGListKit
final class ImageCellHeightCache: IssueCommentImageHeightCellDelegate {
// width is unused since the full size of the image is stored
private static let cache = WidthCache<URL, CGSize>()
private weak var sectionController: ListSectionController?
init(sectionController: ListSectionController) {
self.sectionController = sectionController
}
// MARK: Public API
func height(model: IssueCommentImageModel, width: CGFloat) -> CGFloat {
guard let size = ImageCellHeightCache.cache.data(key: model.url, width: 0) else { return 200 }
return BoundedImageSize(originalSize: size, containerWidth: width).height
}
// MARK: IssueCommentImageHeightCellDelegate
func imageDidFinishLoad(cell: IssueCommentImageCell, url: URL, size: CGSize) {
guard let sectionController = self.sectionController,
sectionController.section != NSNotFound,
size != ImageCellHeightCache.cache.data(key: url, width: 0)
else { return }
ImageCellHeightCache.cache.set(data: size, key: url, width: 0)
UIView.performWithoutAnimation {
sectionController.collectionContext?.invalidateLayout(for: sectionController)
}
}
}