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 2 months ago
parent 9abb38594b
commit ebe3401f31

@ -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: 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: -
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: 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
}
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
}
}

Loading…
Cancel
Save