From d96a1eacec474a5dcb0554c8c2eb10184319d6df Mon Sep 17 00:00:00 2001 From: hyun99999 Date: Wed, 18 Oct 2023 14:49:40 +0900 Subject: [PATCH 1/3] Use UIApplication instead of UIScreen.main on iOS 13.0 and later --- .../YPImagePickerConfiguration.swift | 10 +++++++++- Source/Filters/Photo/YPFiltersView.swift | 11 ++++++++++- Source/Filters/Photo/YPPhotoFiltersVC.swift | 11 ++++++++++- Source/Pages/Gallery/Album/YPAlbumsManager.swift | 10 +++++++++- Source/Pages/Gallery/LibraryMediaManager.swift | 12 +++++++++++- Source/Pages/Gallery/YPLibraryView.swift | 15 +++++++++++++-- Source/Pages/Photo/YPCameraView.swift | 11 ++++++++++- 7 files changed, 72 insertions(+), 8 deletions(-) diff --git a/Source/Configuration/YPImagePickerConfiguration.swift b/Source/Configuration/YPImagePickerConfiguration.swift index af6f6d4e5..327678826 100644 --- a/Source/Configuration/YPImagePickerConfiguration.swift +++ b/Source/Configuration/YPImagePickerConfiguration.swift @@ -20,7 +20,15 @@ public struct YPImagePickerConfiguration { public static var widthOniPad: CGFloat = -1 public static var screenWidth: CGFloat { - var screenWidth: CGFloat = UIScreen.main.bounds.width + var screenWidth: CGFloat = 0 + + if #available(iOS 13.0, *) { + let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene + screenWidth = windowScene?.screen.bounds.width ?? .zero + } else { + screenWidth = UIScreen.main.bounds.width + } + if UIDevice.current.userInterfaceIdiom == .pad && YPImagePickerConfiguration.widthOniPad > 0 { screenWidth = YPImagePickerConfiguration.widthOniPad } diff --git a/Source/Filters/Photo/YPFiltersView.swift b/Source/Filters/Photo/YPFiltersView.swift index da1337c7c..47f19f522 100644 --- a/Source/Filters/Photo/YPFiltersView.swift +++ b/Source/Filters/Photo/YPFiltersView.swift @@ -32,7 +32,16 @@ class YPFiltersView: UIView { ) ) - let isIphone4 = UIScreen.main.bounds.height == 480 + var height: CGFloat = 0 + + if #available(iOS 13.0, *) { + let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene + height = windowScene?.screen.bounds.height ?? .zero + } else { + height = UIScreen.main.bounds.height + } + + let isIphone4 = height == 480 let sideMargin: CGFloat = isIphone4 ? 20 : 0 |-sideMargin-imageView.top(0)-sideMargin-| diff --git a/Source/Filters/Photo/YPPhotoFiltersVC.swift b/Source/Filters/Photo/YPPhotoFiltersVC.swift index e8917121c..508464dd3 100644 --- a/Source/Filters/Photo/YPPhotoFiltersVC.swift +++ b/Source/Filters/Photo/YPPhotoFiltersVC.swift @@ -126,7 +126,16 @@ open class YPPhotoFiltersVC: UIViewController, IsMediaFilterVC, UIGestureRecogni fileprivate func thumbFromImage(_ img: UIImage) -> CIImage { let k = img.size.width / img.size.height - let scale = UIScreen.main.scale + + var scale: CGFloat = 0 + + if #available(iOS 13.0, *) { + let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene + scale = windowScene?.screen.scale ?? .zero + } else { + scale = UIScreen.main.scale + } + let thumbnailHeight: CGFloat = 300 * scale let thumbnailWidth = thumbnailHeight * k let thumbnailSize = CGSize(width: thumbnailWidth, height: thumbnailHeight) diff --git a/Source/Pages/Gallery/Album/YPAlbumsManager.swift b/Source/Pages/Gallery/Album/YPAlbumsManager.swift index aa6b74c7c..c875ea1bf 100644 --- a/Source/Pages/Gallery/Album/YPAlbumsManager.swift +++ b/Source/Pages/Gallery/Album/YPAlbumsManager.swift @@ -36,7 +36,15 @@ class YPAlbumsManager { if album.numberOfItems > 0 { let r = PHAsset.fetchKeyAssets(in: assetCollection, options: nil) if let first = r?.firstObject { - let deviceScale = UIScreen.main.scale + var deviceScale: CGFloat = 0 + + if #available(iOS 13.0, *) { + let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene + deviceScale = windowScene?.screen.scale ?? .zero + } else { + deviceScale = UIScreen.main.scale + } + let targetSize = CGSize(width: 78*deviceScale, height: 78*deviceScale) let options = PHImageRequestOptions() options.isSynchronous = true diff --git a/Source/Pages/Gallery/LibraryMediaManager.swift b/Source/Pages/Gallery/LibraryMediaManager.swift index 9b10a56ea..9b0d8d3f6 100644 --- a/Source/Pages/Gallery/LibraryMediaManager.swift +++ b/Source/Pages/Gallery/LibraryMediaManager.swift @@ -40,7 +40,17 @@ class LibraryMediaManager { func updateCachedAssets(in collectionView: UICollectionView) { let screenWidth = YPImagePickerConfiguration.screenWidth - let size = screenWidth / 4 * UIScreen.main.scale + + var scale: CGFloat = 0 + + if #available(iOS 13.0, *) { + let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene + scale = windowScene?.screen.scale ?? .zero + } else { + scale = UIScreen.main.scale + } + + let size = screenWidth / 4 * scale let cellSize = CGSize(width: size, height: size) var preheatRect = collectionView.bounds diff --git a/Source/Pages/Gallery/YPLibraryView.swift b/Source/Pages/Gallery/YPLibraryView.swift index d42db8866..9bb207ebd 100644 --- a/Source/Pages/Gallery/YPLibraryView.swift +++ b/Source/Pages/Gallery/YPLibraryView.swift @@ -156,11 +156,22 @@ internal final class YPLibraryView: UIView { } func cellSize() -> CGSize { - var screenWidth: CGFloat = UIScreen.main.bounds.width + var screenWidth: CGFloat = 0 + var scale: CGFloat = 0 + + if #available(iOS 13.0, *) { + let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene + screenWidth = windowScene?.screen.bounds.width ?? .zero + scale = windowScene?.screen.scale ?? .zero + } else { + screenWidth = UIScreen.main.bounds.width + scale = UIScreen.main.scale + } + if UIDevice.current.userInterfaceIdiom == .pad && YPImagePickerConfiguration.widthOniPad > 0 { screenWidth = YPImagePickerConfiguration.widthOniPad } - let size = screenWidth / 4 * UIScreen.main.scale + let size = screenWidth / 4 * scale return CGSize(width: size, height: size) } diff --git a/Source/Pages/Photo/YPCameraView.swift b/Source/Pages/Photo/YPCameraView.swift index 451e7240f..141977e34 100644 --- a/Source/Pages/Photo/YPCameraView.swift +++ b/Source/Pages/Photo/YPCameraView.swift @@ -50,7 +50,16 @@ internal class YPCameraView: UIView, UIGestureRecognizerDelegate { } // Layout - let isIphone4 = UIScreen.main.bounds.height == 480 + var height: CGFloat = 0 + + if #available(iOS 13.0, *) { + let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene + height = windowScene?.screen.bounds.height ?? .zero + } else { + height = UIScreen.main.bounds.height + } + + let isIphone4 = height == 480 let sideMargin: CGFloat = isIphone4 ? 20 : 0 if YPConfig.onlySquareImagesFromCamera { layout( From ada0cef9487541615b47cc854e568d73b0f1b52a Mon Sep 17 00:00:00 2001 From: hyun99999 Date: Thu, 19 Oct 2023 01:17:35 +0900 Subject: [PATCH 2/3] Fix .zero to 1.0 ensure minimal operation through the scale factor. --- Source/Configuration/YPImagePickerConfiguration.swift | 2 +- Source/Pages/Gallery/Album/YPAlbumsManager.swift | 2 +- Source/Pages/Gallery/LibraryMediaManager.swift | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/Configuration/YPImagePickerConfiguration.swift b/Source/Configuration/YPImagePickerConfiguration.swift index 327678826..693a2956a 100644 --- a/Source/Configuration/YPImagePickerConfiguration.swift +++ b/Source/Configuration/YPImagePickerConfiguration.swift @@ -24,7 +24,7 @@ public struct YPImagePickerConfiguration { if #available(iOS 13.0, *) { let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene - screenWidth = windowScene?.screen.bounds.width ?? .zero + screenWidth = windowScene?.screen.bounds.width ?? 1.0 } else { screenWidth = UIScreen.main.bounds.width } diff --git a/Source/Pages/Gallery/Album/YPAlbumsManager.swift b/Source/Pages/Gallery/Album/YPAlbumsManager.swift index c875ea1bf..4163641e2 100644 --- a/Source/Pages/Gallery/Album/YPAlbumsManager.swift +++ b/Source/Pages/Gallery/Album/YPAlbumsManager.swift @@ -40,7 +40,7 @@ class YPAlbumsManager { if #available(iOS 13.0, *) { let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene - deviceScale = windowScene?.screen.scale ?? .zero + deviceScale = windowScene?.screen.scale ?? 1.0 } else { deviceScale = UIScreen.main.scale } diff --git a/Source/Pages/Gallery/LibraryMediaManager.swift b/Source/Pages/Gallery/LibraryMediaManager.swift index 9b0d8d3f6..57791c9dc 100644 --- a/Source/Pages/Gallery/LibraryMediaManager.swift +++ b/Source/Pages/Gallery/LibraryMediaManager.swift @@ -45,7 +45,7 @@ class LibraryMediaManager { if #available(iOS 13.0, *) { let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene - scale = windowScene?.screen.scale ?? .zero + scale = windowScene?.screen.scale ?? 1.0 } else { scale = UIScreen.main.scale } From 73dde470b3f300c6a8634ab9c5944bb813399c23 Mon Sep 17 00:00:00 2001 From: hyun99999 Date: Thu, 19 Oct 2023 01:20:11 +0900 Subject: [PATCH 3/3] Use view.windowScene.screen to reduce a length --- Source/Filters/Photo/YPFiltersView.swift | 3 +-- Source/Filters/Photo/YPPhotoFiltersVC.swift | 3 +-- Source/Pages/Gallery/YPLibraryView.swift | 5 ++--- Source/Pages/Photo/YPCameraView.swift | 3 +-- 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/Source/Filters/Photo/YPFiltersView.swift b/Source/Filters/Photo/YPFiltersView.swift index 47f19f522..d9b6c7aca 100644 --- a/Source/Filters/Photo/YPFiltersView.swift +++ b/Source/Filters/Photo/YPFiltersView.swift @@ -35,8 +35,7 @@ class YPFiltersView: UIView { var height: CGFloat = 0 if #available(iOS 13.0, *) { - let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene - height = windowScene?.screen.bounds.height ?? .zero + height = window?.windowScene?.screen.bounds.height ?? .zero } else { height = UIScreen.main.bounds.height } diff --git a/Source/Filters/Photo/YPPhotoFiltersVC.swift b/Source/Filters/Photo/YPPhotoFiltersVC.swift index 508464dd3..c82a4918f 100644 --- a/Source/Filters/Photo/YPPhotoFiltersVC.swift +++ b/Source/Filters/Photo/YPPhotoFiltersVC.swift @@ -130,8 +130,7 @@ open class YPPhotoFiltersVC: UIViewController, IsMediaFilterVC, UIGestureRecogni var scale: CGFloat = 0 if #available(iOS 13.0, *) { - let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene - scale = windowScene?.screen.scale ?? .zero + scale = window?.windowScene?.screen.scale ?? 1.0 } else { scale = UIScreen.main.scale } diff --git a/Source/Pages/Gallery/YPLibraryView.swift b/Source/Pages/Gallery/YPLibraryView.swift index 9bb207ebd..09f39e147 100644 --- a/Source/Pages/Gallery/YPLibraryView.swift +++ b/Source/Pages/Gallery/YPLibraryView.swift @@ -160,9 +160,8 @@ internal final class YPLibraryView: UIView { var scale: CGFloat = 0 if #available(iOS 13.0, *) { - let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene - screenWidth = windowScene?.screen.bounds.width ?? .zero - scale = windowScene?.screen.scale ?? .zero + screenWidth = window?.windowScene?.screen.bounds.width ?? 1.0 + scale = window?.windowScene?.screen.scale ?? 1.0 } else { screenWidth = UIScreen.main.bounds.width scale = UIScreen.main.scale diff --git a/Source/Pages/Photo/YPCameraView.swift b/Source/Pages/Photo/YPCameraView.swift index 141977e34..01cd1c528 100644 --- a/Source/Pages/Photo/YPCameraView.swift +++ b/Source/Pages/Photo/YPCameraView.swift @@ -53,8 +53,7 @@ internal class YPCameraView: UIView, UIGestureRecognizerDelegate { var height: CGFloat = 0 if #available(iOS 13.0, *) { - let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene - height = windowScene?.screen.bounds.height ?? .zero + height = window?.windowScene?.screen.bounds.height ?? .zero } else { height = UIScreen.main.bounds.height }