Refactor image loading logic in QRCodeStyleModels and QRCodeStyleView to streamline resource retrieval from various directories, enhancing flexibility in image sourcing for QR code styles.

main
v504 10 months ago
parent 9abb38594b
commit ebe3401f31

@ -399,4 +399,34 @@ enum QRCodeLogo: String, CaseIterable, Hashable {
case .facebook: return "Facebook" 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: BundleResources
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
}
} }

@ -376,16 +376,38 @@ struct QRCodeStyleView: View {
// MARK: - // MARK: -
private func loadImage(named name: String) -> UIImage? { private func loadImage(named name: String) -> UIImage? {
// Bundle // 1: Bundle
if let path = Bundle.main.path(forResource: name, ofType: "png", inDirectory: "Resources/dots") { if let image = UIImage(named: name) {
return UIImage(contentsOfFile: path) return image
} }
if let path = Bundle.main.path(forResource: name, ofType: "png", inDirectory: "Resources/eyes") {
// 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) return UIImage(contentsOfFile: path)
} }
if let path = Bundle.main.path(forResource: name, ofType: "png", inDirectory: "Resources/logos") { }
// 3: BundleResources
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
}
// 5: Bundle
if let path = Bundle.main.path(forResource: name, ofType: "png") {
return UIImage(contentsOfFile: path) return UIImage(contentsOfFile: path)
} }
return nil return nil
} }
} }

Loading…
Cancel
Save