@ -376,16 +376,38 @@ struct QRCodeStyleView: View {
// MARK: - 辅 助 函 数
// MARK: - 辅 助 函 数
private func loadImage ( named name : String ) -> UIImage ? {
private func loadImage ( named name : String ) -> UIImage ? {
// 尝试 从 B u n d l e 中 加载 图 片
// 方法 1 : 尝试 从 B u n d l e 中 直接 加 载
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 " ) {
return UIImage ( contentsOfFile : path )
// 方 法 2 : 尝 试 从 R e s o u r c e s 子 目 录 加 载
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 : 尝 试 从 B u n d l e 的 R e s o u r c e s 目 录 加 载
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 : 尝 试 从 A s s e t s . x c a s s e t s 加 载
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 : 尝 试 从 B u n d l e 根 目 录 加 载
if let path = Bundle . main . path ( forResource : name , ofType : " png " ) {
return UIImage ( contentsOfFile : path )
return UIImage ( contentsOfFile : path )
}
}
return nil
return nil
}
}
}
}