From 36157b53f9c44dd637b82b87f2eca2b8c5eafcb8 Mon Sep 17 00:00:00 2001 From: Vegard Bieker Matthey Date: Thu, 16 Jul 2026 17:45:21 +0200 Subject: [PATCH] remove unnecessary override of viewWillTransition and only load image only once when view size has changed --- ImageViewer/ViewController.swift | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/ImageViewer/ViewController.swift b/ImageViewer/ViewController.swift index 1cf71d7..468a3c6 100644 --- a/ImageViewer/ViewController.swift +++ b/ImageViewer/ViewController.swift @@ -58,7 +58,6 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { @IBOutlet var scrollingCollectionView: UICollectionView! var scrollPos: CGPoint! var hasSetContentOffset = false - var pageCount = 0 var imageView = UIImageView() var mode = PageTurnMode.leftToRight @@ -67,8 +66,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { var currentPage: Int! var progress = ProgressIndices(v: 0, c: 0, i: 0) var currentPath: URL! - - var changedOrientation = false + var lastLayoutSize: CGSize = .zero var leftTap: UITapGestureRecognizer! var rightTap: UITapGestureRecognizer! @@ -673,6 +671,8 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() + guard view.bounds.size != lastLayoutSize else { return } + lastLayoutSize = UIScreen.main.bounds.size if scrollingCollectionView.isHidden == true || mode != .scroll || scrollingCollectionView.contentSize == .zero { @@ -695,8 +695,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { } scrollingCollectionView.setContentOffset(offset, animated: false) hasSetContentOffset = true - } else if changedOrientation { - changedOrientation = false + } else { let screenSize = UIScreen.main.bounds.size var offset = scrollingCollectionView.contentOffset offset.y *= (screenSize.width / screenSize.height) @@ -1339,24 +1338,6 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { } return nil } - - override func viewWillTransition( - to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator - ) { - super.viewWillTransition(to: size, with: coordinator) - if mode != .scroll { - imageLoader.loadImage( - archiveURL: getArchiveURL(progress.v), - filename: getImagePath(progress), - scaling: .scaleAspectFit, - screenSize: size - ) { image in - self.imageView.image = image - } - } else { - changedOrientation = true - } - } } func getGlobalState() -> GlobalState {