From ebe3401f31be65d84be6ff4880e3d47ad4fb482d Mon Sep 17 00:00:00 2001 From: v504 Date: Mon, 25 Aug 2025 17:50:48 +0800 Subject: [PATCH] Refactor image loading logic in QRCodeStyleModels and QRCodeStyleView to streamline resource retrieval from various directories, enhancing flexibility in image sourcing for QR code styles. --- MyQrCode/Models/QRCodeStyleModels.swift | 30 ++++++++++++++++++++++ MyQrCode/Views/QRCodeStyleView.swift | 34 ++++++++++++++++++++----- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/MyQrCode/Models/QRCodeStyleModels.swift b/MyQrCode/Models/QRCodeStyleModels.swift index 649f231..f40cfda 100644 --- a/MyQrCode/Models/QRCodeStyleModels.swift +++ b/MyQrCode/Models/QRCodeStyleModels.swift @@ -399,4 +399,34 @@ enum QRCodeLogo: String, CaseIterable, Hashable { case .facebook: return "Facebook" } } + + var image: UIImage? { + // 方法1: 尝试从Bundle中直接加载 + if let image = UIImage(named: rawValue) { + return image + } + + // 方法2: 尝试从logos子目录加载 + if let image = UIImage(named: "logos/\(rawValue)") { + return image + } + + // 方法3: 尝试从Resources/logos路径加载 + if let path = Bundle.main.path(forResource: rawValue, ofType: "png", inDirectory: "Resources/logos") { + return UIImage(contentsOfFile: path) + } + + // 方法4: 尝试从Bundle的Resources目录加载 + if let bundlePath = Bundle.main.path(forResource: "Resources", ofType: nil), + let imagePath = Bundle.main.path(forResource: rawValue, ofType: "png", inDirectory: "logos") { + return UIImage(contentsOfFile: imagePath) + } + + // 方法5: 尝试从Assets.xcassets加载 + if let image = UIImage(named: rawValue, in: Bundle.main, with: nil) { + return image + } + + return nil + } } diff --git a/MyQrCode/Views/QRCodeStyleView.swift b/MyQrCode/Views/QRCodeStyleView.swift index 0c1b8d6..830ed36 100644 --- a/MyQrCode/Views/QRCodeStyleView.swift +++ b/MyQrCode/Views/QRCodeStyleView.swift @@ -376,16 +376,38 @@ struct QRCodeStyleView: View { // MARK: - 辅助函数 private func loadImage(named name: String) -> UIImage? { - // 尝试从Bundle中加载图片 - if let path = Bundle.main.path(forResource: name, ofType: "png", inDirectory: "Resources/dots") { - return UIImage(contentsOfFile: path) + // 方法1: 尝试从Bundle中直接加载 + if let image = UIImage(named: name) { + return image } - if let path = Bundle.main.path(forResource: name, ofType: "png", inDirectory: "Resources/eyes") { - return UIImage(contentsOfFile: path) + + // 方法2: 尝试从Resources子目录加载 + let subdirectories = ["dots", "eyes", "logos"] + for subdirectory in subdirectories { + if let path = Bundle.main.path(forResource: name, ofType: "png", inDirectory: "Resources/\(subdirectory)") { + return UIImage(contentsOfFile: path) + } + } + + // 方法3: 尝试从Bundle的Resources目录加载 + if let bundlePath = Bundle.main.path(forResource: "Resources", ofType: nil) { + for subdirectory in subdirectories { + if let imagePath = Bundle.main.path(forResource: name, ofType: "png", inDirectory: subdirectory) { + return UIImage(contentsOfFile: imagePath) + } + } + } + + // 方法4: 尝试从Assets.xcassets加载 + if let image = UIImage(named: name, in: Bundle.main, with: nil) { + return image } - if let path = Bundle.main.path(forResource: name, ofType: "png", inDirectory: "Resources/logos") { + + // 方法5: 尝试从Bundle根目录加载 + if let path = Bundle.main.path(forResource: name, ofType: "png") { return UIImage(contentsOfFile: path) } + return nil } }