From 51c1498e92512ef474463ec9aa95afc449b01569 Mon Sep 17 00:00:00 2001 From: Silence <294005139@qq.com> Date: Fri, 16 Feb 2024 16:47:51 +0800 Subject: [PATCH] chore: release version 4.1.6 --- .../Core/Extension/Core+String.swift | 4 ++ .../Core/Extension/Core+UIFont.swift | 4 ++ .../Core/Extension/Core+UIImage.swift | 51 ++++++++++--------- .../HXPhotoPicker/Core/HXPhotoPicker.swift | 9 ++++ .../Core/Util/ImageResource.swift | 6 ++- .../Core/Util/PhotoTools+Alert.swift | 2 + .../HXPhotoPicker/Core/Util/PhotoTools.swift | 2 +- .../HXPhotoPicker/Core/Util/TextManager.swift | 22 +++----- .../View/EditorAdjusterView+Croper.swift | 40 +++++++-------- .../EditorChartletViewController.swift | 4 +- .../View/Filter/EditorFiltersView.swift | 2 +- .../Config/PhotoListConfiguration.swift | 4 +- .../PickerBottomViewConfiguration.swift | 2 + .../Picker/Model/PhotoAsset+Local.swift | 3 +- .../Picker/Model/PickerTypes.swift | 10 ---- .../ToolBar/PhotoPreviewSelectedView.swift | 22 +++++++- 16 files changed, 109 insertions(+), 78 deletions(-) diff --git a/Sources/HXPhotoPicker/Core/Extension/Core+String.swift b/Sources/HXPhotoPicker/Core/Extension/Core+String.swift index c4a4d19f..59050276 100644 --- a/Sources/HXPhotoPicker/Core/Extension/Core+String.swift +++ b/Sources/HXPhotoPicker/Core/Extension/Core+String.swift @@ -32,10 +32,13 @@ extension String: HXPickerCompatibleValue { .shared } + #if HXPICKER_ENABLE_PICKER || HXPICKER_ENABLE_CAMERA static var textNotAuthorized: HX.TextManager.Picker.NotAuthorized { textManager.picker.notAuthorized } + #endif + #if HXPICKER_ENABLE_PICKER static var textPhotoList: HX.TextManager.Picker.PhotoList { textManager.picker.photoList } @@ -43,6 +46,7 @@ extension String: HXPickerCompatibleValue { static var textPreview: HX.TextManager.Picker.Preview { textManager.picker.preview } + #endif var lrc: String? { var lrcString: String? diff --git a/Sources/HXPhotoPicker/Core/Extension/Core+UIFont.swift b/Sources/HXPhotoPicker/Core/Extension/Core+UIFont.swift index 3610acac..bda40f7b 100644 --- a/Sources/HXPhotoPicker/Core/Extension/Core+UIFont.swift +++ b/Sources/HXPhotoPicker/Core/Extension/Core+UIFont.swift @@ -14,10 +14,13 @@ extension UIFont: HXPickerCompatibleValue { HX.TextManager.shared } + #if HXPICKER_ENABLE_PICKER || HXPICKER_ENABLE_CAMERA static var textNotAuthorized: HX.TextManager.Picker.NotAuthorized { textManager.picker.notAuthorized } + #endif + #if HXPICKER_ENABLE_PICKER static var textPhotoList: HX.TextManager.Picker.PhotoList { textManager.picker.photoList } @@ -25,6 +28,7 @@ extension UIFont: HXPickerCompatibleValue { static var textPreview: HX.TextManager.Picker.Preview { textManager.picker.preview } + #endif static func regularPingFang(ofSize size: CGFloat) -> UIFont { if let font = UIFont.init(name: "PingFangSC-Regular", size: size) { diff --git a/Sources/HXPhotoPicker/Core/Extension/Core+UIImage.swift b/Sources/HXPhotoPicker/Core/Extension/Core+UIImage.swift index 3ee6b759..3c25e0d2 100644 --- a/Sources/HXPhotoPicker/Core/Extension/Core+UIImage.swift +++ b/Sources/HXPhotoPicker/Core/Extension/Core+UIImage.swift @@ -47,46 +47,51 @@ extension UIImage { } return self.scaleToFillSize(size: imageSize) } + func scaleToFillSize(size: CGSize, mode: HX.ImageTargetMode = .fill, scale: CGFloat = 0) -> UIImage? { if __CGSizeEqualToSize(self.size, size) { return self } - let scale = scale == 0 ? self.scale : scale - let isEqualRatio = size.width / size.height != width / height let rect: CGRect - if !isEqualRatio && mode == .fill { - let scale = size.width / width - var scaleHeight = scale * height - var scaleWidth = size.width - if scaleHeight < size.height { - scaleWidth = size.height / scaleHeight * size.width - scaleHeight = size.height + let rendererSize: CGSize + if mode == .fill { + let isEqualRatio = size.width / size.height == width / height + if isEqualRatio { + rendererSize = size + rect = CGRect(origin: .zero, size: size) + }else { + let scale = size.width / width + var scaleHeight = scale * height + var scaleWidth = size.width + if scaleHeight < size.height { + scaleWidth = size.height / scaleHeight * size.width + scaleHeight = size.height + } + rendererSize = .init(width: scaleWidth, height: scaleHeight) + rect = .init(origin: .zero, size: rendererSize) } - rect = CGRect( - x: -(scaleWidth - size.height) * 0.5, - y: -(scaleHeight - size.height) * 0.5, - width: scaleWidth, - height: scaleHeight - ) }else { + rendererSize = size if mode == .fit { rect = CGRect(origin: .zero, size: size) }else { let scale = size.width / width - let scaleHeight = scale * size.height - let scaleWidth = size.width + var scaleHeight = scale * height + if scaleHeight < size.height { + scaleHeight = size.height + } rect = CGRect( - x: -(width - scaleWidth) * 0.5, - y: -(height - scaleHeight) * 0.5, - width: width, - height: height + x: 0, + y: -(scaleHeight - size.height) / 2, + width: size.width, + height: scaleHeight ) } } let format = UIGraphicsImageRendererFormat() format.opaque = false - format.scale = scale - let renderer = UIGraphicsImageRenderer(size: size, format: format) + format.scale = scale == 0 ? self.scale : scale + let renderer = UIGraphicsImageRenderer(size: rendererSize, format: format) let image = renderer.image { context in draw(in: rect) } diff --git a/Sources/HXPhotoPicker/Core/HXPhotoPicker.swift b/Sources/HXPhotoPicker/Core/HXPhotoPicker.swift index 4f855ce0..05302661 100644 --- a/Sources/HXPhotoPicker/Core/HXPhotoPicker.swift +++ b/Sources/HXPhotoPicker/Core/HXPhotoPicker.swift @@ -221,6 +221,15 @@ public enum HX { try await Photo.capture(config, type: type, delegate: delegate, fromVC: fromVC) } #endif + + public enum ImageTargetMode { + /// 与原图宽高比一致,高度会根据`targetSize`计算 + case fill + /// 根据`targetSize`拉伸/缩放 + case fit + /// 如果`targetSize`的比例与原图不一样则居中显示 + case center + } } public struct HXPickerWrapper { diff --git a/Sources/HXPhotoPicker/Core/Util/ImageResource.swift b/Sources/HXPhotoPicker/Core/Util/ImageResource.swift index 1ea3f688..4da7ce1f 100644 --- a/Sources/HXPhotoPicker/Core/Util/ImageResource.swift +++ b/Sources/HXPhotoPicker/Core/Util/ImageResource.swift @@ -20,7 +20,7 @@ public extension HX { public var picker: Picker = .init() #endif - #if HXPICKER_ENABLE_EDITOR + #if HXPICKER_ENABLE_EDITOR || HXPICKER_ENABLE_EDITOR_VIEW /// 编辑器 public var editor: Editor = .init() #endif @@ -138,7 +138,7 @@ public extension HX.ImageResource { } #endif - #if HXPICKER_ENABLE_EDITOR + #if HXPICKER_ENABLE_EDITOR || HXPICKER_ENABLE_EDITOR_VIEW struct Editor { /// 工具栏 public var tools: Tools = .init() @@ -231,6 +231,8 @@ public extension HX.ImageResource { public var back: String = "hx_photo_edit_pull_down" /// 跳转相册按钮 public var album: String = "hx_editor_tools_chartle_album" + /// 相册为空时的封面图片 + public var albumEmptyCover: String = "hx_picker_album_empty" /// 贴纸删除按钮 public var delete: String = "hx_editor_view_sticker_item_delete" /// 贴纸旋转按钮 diff --git a/Sources/HXPhotoPicker/Core/Util/PhotoTools+Alert.swift b/Sources/HXPhotoPicker/Core/Util/PhotoTools+Alert.swift index 17d06383..3e96ceca 100644 --- a/Sources/HXPhotoPicker/Core/Util/PhotoTools+Alert.swift +++ b/Sources/HXPhotoPicker/Core/Util/PhotoTools+Alert.swift @@ -124,6 +124,7 @@ extension PhotoTools { } /// 显示没有相机权限弹窗 + #if HXPICKER_ENABLE_PICKER || HXPICKER_ENABLE_CAMERA static func showNotCameraAuthorizedAlert( viewController: UIViewController?, cancelHandler: (() -> Void)? = nil @@ -141,4 +142,5 @@ extension PhotoTools { openSettingsURL() } } + #endif } diff --git a/Sources/HXPhotoPicker/Core/Util/PhotoTools.swift b/Sources/HXPhotoPicker/Core/Util/PhotoTools.swift index 0a336768..9fb71805 100644 --- a/Sources/HXPhotoPicker/Core/Util/PhotoTools.swift +++ b/Sources/HXPhotoPicker/Core/Util/PhotoTools.swift @@ -525,7 +525,7 @@ public struct PhotoTools { return layer } - #if HXPICKER_ENABLE_EDITOR || HXPICKER_ENABLE_CAMERA + #if HXPICKER_ENABLE_EDITOR || HXPICKER_ENABLE_EDITOR_VIEW || HXPICKER_ENABLE_CAMERA static func getColor(red: Int, green: Int, blue: Int, alpha: Int = 255) -> CIColor { return CIColor(red: CGFloat(Double(red) / 255.0), green: CGFloat(Double(green) / 255.0), diff --git a/Sources/HXPhotoPicker/Core/Util/TextManager.swift b/Sources/HXPhotoPicker/Core/Util/TextManager.swift index f498bcc9..d7e76fdc 100644 --- a/Sources/HXPhotoPicker/Core/Util/TextManager.swift +++ b/Sources/HXPhotoPicker/Core/Util/TextManager.swift @@ -15,23 +15,17 @@ public extension HX { class TextManager { public static let shared = TextManager() - #if HXPICKER_ENABLE_PICKER + #if HXPICKER_ENABLE_PICKER || HXPICKER_ENABLE_CAMERA /// 选择器 public var picker: Picker = .init() - #endif - - #if HXPICKER_ENABLE_EDITOR - /// 编辑器 - public var editor: Editor = .init() - #endif - - #if HXPICKER_ENABLE_CAMERA /// 相机 public var camera: Camera = .init() + public var cameraNotAuthorized: CameraNotAuthorized = .init() #endif - #if HXPICKER_ENABLE_PICKER || HXPICKER_ENABLE_CAMERA - public var cameraNotAuthorized: CameraNotAuthorized = .init() + #if HXPICKER_ENABLE_EDITOR || HXPICKER_ENABLE_EDITOR_VIEW + /// 编辑器 + public var editor: Editor = .init() #endif } } @@ -54,7 +48,7 @@ public extension HX.TextManager { } } - #if HXPICKER_ENABLE_PICKER + #if HXPICKER_ENABLE_PICKER || HXPICKER_ENABLE_CAMERA struct Picker { /// 相册未授权 public var notAuthorized: NotAuthorized = .init() @@ -191,7 +185,7 @@ public extension HX.TextManager { } #endif - #if HXPICKER_ENABLE_EDITOR + #if HXPICKER_ENABLE_EDITOR || HXPICKER_ENABLE_EDITOR_VIEW struct Editor { public var tools: Tools = .init() public var brush: Tools = .init() @@ -295,7 +289,7 @@ public extension HX.TextManager { } #endif - #if HXPICKER_ENABLE_CAMERA + #if HXPICKER_ENABLE_PICKER || HXPICKER_ENABLE_CAMERA struct Camera { public var unavailableTitle: TextType = .localized("相机不可用!") diff --git a/Sources/HXPhotoPicker/Editor+View/View/EditorAdjusterView+Croper.swift b/Sources/HXPhotoPicker/Editor+View/View/EditorAdjusterView+Croper.swift index 1925abca..50709ce8 100644 --- a/Sources/HXPhotoPicker/Editor+View/View/EditorAdjusterView+Croper.swift +++ b/Sources/HXPhotoPicker/Editor+View/View/EditorAdjusterView+Croper.swift @@ -220,7 +220,9 @@ extension EditorAdjusterView { imageData, compressionQuality: compressionQuality ) { [weak self] data in - guard let self = self, let data = data else { + guard let self = self, + let data = data, + let image = UIImage(data: data)?.normalizedImage() else { DispatchQueue.main.async { completion(.failure(EditorError.error(type: .compressionFailed, message: "图片压缩失败"))) } @@ -239,26 +241,24 @@ extension EditorAdjusterView { } return } - self.compressImageData( - data, - compressionQuality: 0.3 - ) { thumbData in - if let thumbData = thumbData, - let thumbnailImage = UIImage(data: thumbData) { - DispatchQueue.main.async { - completion( - .success(.init( - image: thumbnailImage, - urlConfig: urlConfig, - imageType: .normal, - data: self.getData() - )) - ) - } + let thumbImage: UIImage? + if image.width * image.height < 40000 { + thumbImage = image + }else { + thumbImage = image.scaleToFillSize(size: .init(width: 200, height: 200)) + } + DispatchQueue.main.async { + if let thumbImage { + completion( + .success(.init( + image: thumbImage, + urlConfig: urlConfig, + imageType: .normal, + data: self.getData() + )) + ) }else { - DispatchQueue.main.async { - completion(.failure(.error(type: .compressionFailed, message: "封面图片压缩失败"))) - } + completion(.failure(.error(type: .compressionFailed, message: "封面图片压缩失败"))) } } } diff --git a/Sources/HXPhotoPicker/Editor/Controller/EditorChartletViewController.swift b/Sources/HXPhotoPicker/Editor/Controller/EditorChartletViewController.swift index 02ffc046..9df4bac0 100644 --- a/Sources/HXPhotoPicker/Editor/Controller/EditorChartletViewController.swift +++ b/Sources/HXPhotoPicker/Editor/Controller/EditorChartletViewController.swift @@ -136,10 +136,10 @@ public class EditorChartletViewController: BaseViewController, EditorChartletLis if let url = title.url { titleChartlet = EditorChartletTitle(url: url) }else { - titleChartlet = .init(image: .imageResource.picker.albumList.emptyCover.image) + titleChartlet = .init(image: .imageResource.editor.sticker.albumEmptyCover.image) } #else - titleChartlet = .init(image: .imageResource.picker.albumList.emptyCover.image) + titleChartlet = .init(image: .imageResource.editor.sticker.albumEmptyCover.image) #endif } if index == 0 { diff --git a/Sources/HXPhotoPicker/Editor/View/Filter/EditorFiltersView.swift b/Sources/HXPhotoPicker/Editor/View/Filter/EditorFiltersView.swift index e006ef1d..350b1b6e 100644 --- a/Sources/HXPhotoPicker/Editor/View/Filter/EditorFiltersView.swift +++ b/Sources/HXPhotoPicker/Editor/View/Filter/EditorFiltersView.swift @@ -83,7 +83,7 @@ class EditorFiltersView: UIView { operation.addExecutionBlock { [unowned operation, weak self] in guard let self = self else { return } if operation.isCancelled { return } - self.image = originalImage.scaleToFillSize(size: CGSize(width: 80, height: 80)) + self.image = originalImage.scaleToFillSize(size: CGSize(width: 80, height: 80), mode: .center) if operation.isCancelled { return } DispatchQueue.main.async { self.didLoad = true diff --git a/Sources/HXPhotoPicker/Picker/Config/PhotoListConfiguration.swift b/Sources/HXPhotoPicker/Picker/Config/PhotoListConfiguration.swift index d632c963..07761170 100644 --- a/Sources/HXPhotoPicker/Picker/Config/PhotoListConfiguration.swift +++ b/Sources/HXPhotoPicker/Picker/Config/PhotoListConfiguration.swift @@ -186,11 +186,13 @@ public struct PhotoListConfiguration { bottomView.setThemeColor(color) cell.setThemeColor(color) assetNumber.setThemeColor(color) + #if HXPICKER_ENABLE_CAMERA && !targetEnvironment(macCatalyst) if var cameraConfig = cameraType.customConfig { cameraConfig.tintColor = color cameraType = .custom(cameraConfig) } - filterThemeColor = color + #endif + filterThemeColor = color filterThemeDarkColor = color } } diff --git a/Sources/HXPhotoPicker/Picker/Config/PickerBottomViewConfiguration.swift b/Sources/HXPhotoPicker/Picker/Config/PickerBottomViewConfiguration.swift index 7520aa64..d7849b8e 100644 --- a/Sources/HXPhotoPicker/Picker/Config/PickerBottomViewConfiguration.swift +++ b/Sources/HXPhotoPicker/Picker/Config/PickerBottomViewConfiguration.swift @@ -212,7 +212,9 @@ public struct PickerBottomViewConfiguration { finishButtonDarkBackgroundColor = color finishButtonDisableBackgroundColor = color.withAlphaComponent(0.4) finishButtonDisableDarkBackgroundColor = color.withAlphaComponent(0.4) + #if HXPICKER_ENABLE_EDITOR editButtonTitleColor = color + #endif promptIconColor = color promptIconDarkColor = color promptTitleColor = color diff --git a/Sources/HXPhotoPicker/Picker/Model/PhotoAsset+Local.swift b/Sources/HXPhotoPicker/Picker/Model/PhotoAsset+Local.swift index 4fb1bb7a..52ada684 100644 --- a/Sources/HXPhotoPicker/Picker/Model/PhotoAsset+Local.swift +++ b/Sources/HXPhotoPicker/Picker/Model/PhotoAsset+Local.swift @@ -184,8 +184,7 @@ extension PhotoAsset { size: CGSize( width: targetWidth, height: targetWidth - ), - mode: .fill + ) ) self.localImageAsset?.thumbnail = thumbnail DispatchQueue.main.async { diff --git a/Sources/HXPhotoPicker/Picker/Model/PickerTypes.swift b/Sources/HXPhotoPicker/Picker/Model/PickerTypes.swift index d9123c3b..64b51ab2 100644 --- a/Sources/HXPhotoPicker/Picker/Model/PickerTypes.swift +++ b/Sources/HXPhotoPicker/Picker/Model/PickerTypes.swift @@ -345,14 +345,4 @@ public enum PhotoPreviewType { case browser } -public extension HX { - enum ImageTargetMode { - /// 与原图宽高比一致,高度会根据`targetSize`计算 - case fill - /// 根据`targetSize`拉伸/缩放 - case fit - /// 如果`targetSize`的比例与原图不一样则居中显示 - case center - } -} diff --git a/Sources/HXPhotoPicker/Picker/View/ToolBar/PhotoPreviewSelectedView.swift b/Sources/HXPhotoPicker/Picker/View/ToolBar/PhotoPreviewSelectedView.swift index 25564b4f..edbd32b1 100644 --- a/Sources/HXPhotoPicker/Picker/View/ToolBar/PhotoPreviewSelectedView.swift +++ b/Sources/HXPhotoPicker/Picker/View/ToolBar/PhotoPreviewSelectedView.swift @@ -66,12 +66,27 @@ class PhotoPreviewSelectedView: UIView, reloadSectionInset() addSubview(collectionView) } + + var leftMargin: CGFloat { + if let splitViewController = viewController?.splitViewController as? PhotoSplitViewController, + !UIDevice.isPortrait, + !UIDevice.isPad { + if !splitViewController.isSplitShowColumn { + return UIDevice.leftMargin + }else { + return 0 + } + }else { + return UIDevice.leftMargin + } + } + func reloadSectionInset() { if x == 0 { collectionViewLayout.sectionInset.top = 10 collectionViewLayout.sectionInset.bottom = 5 - if UIDevice.leftMargin > 0 { - collectionViewLayout.sectionInset.left = UIDevice.leftMargin + if leftMargin > 0 { + collectionViewLayout.sectionInset.left = leftMargin }else { collectionViewLayout.sectionInset.left = 12 } @@ -241,6 +256,9 @@ class PhotoPreviewSelectedView: UIView, } override func layoutSubviews() { + if !collectionView.frame.equalTo(bounds) { + reloadSectionInset() + } collectionView.frame = bounds }