He Brian,
First off: Great turorials! Appreciate the hard work!
I had a question regarding the ScreenShotCell imageView and the layout of the sizing of the ScreenShotCells.
I basically found a way to render out the ScreenShot cells size and the PreviewCell size according to the various .scaleAspectFit sizes of the apps’ screenShot images. Considering that either the app preview only has horizontal or vertical images, I fetched the first image and used its’ size as the size for both the ScreenShotCell and the PreviewCell.
Both in AppDetailController and PreviewScreenShotController I added the following:
var app: Result? {
didSet {
self.collectionView.reloadData()
self.screenShotURLs = self.app?.screenshotUrls ?? []
guard let firstUrlString = self.app?.screenshotUrls?.first else { return }
if let url = URL(string: firstUrlString) {
dummyScreenShot.sd_setImage(with: url) { (image, _, _, _) in
self.imageSize = image?.size
}
}
}
}
var imageSize: CGSize? {
didSet {
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
}
let dummyScreenShot = UIImageView()
let verticalImageWidth: CGFloat = 230
- And in sizeForItem, again both in AppDetailController and PreviewScreenShotController:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
guard let previewCellSize = self.imageSize else { return CGSize(width: view.frame.width, height: view.frame.height)}
if previewCellSize.width > previewCellSize.height {
let ratio = previewCellSize.width / previewCellSize.height
return CGSize(width: view.frame.width, height: self.view.frame.width / ratio)
} else {
let ratio = previewCellSize.height / previewCellSize.width
return CGSize(width: verticalImageWidth, height: (verticalImageWidth * ratio))
}
}
This seems all a bit naive to me and I was wondering if there is a more efficient way of doing this? For example in the case of adjusting the collectionView cell sizes according to the intrinsic content size of images with more varying sizes?
Cheers