iOS Framework代码中加载图片资源

在Framework中,加载UIImage(named:"icon.png")时,老是失败,

换成 UIImage(contentsOfFile: filePath) 也是不对:

if let filePath = Bundle.main.path(forResource: "icon", ofType: "png"){

icon = UIImage(contentsOfFile: filePath)

}

后来搜索发现,在Framework中,无法直接根据名字获得,

需要通过framework的bundle来加载

let bundle = Bundle(for: SomeClass.self)

if let filePath = bundle.path(forResource: "icon", ofType: "png"){

icon = UIImage(contentsOfFile: filePath)

}

相关推荐
sweet丶7 小时前
UIWindowScene 使用指南:掌握 iOS 多窗口架构
ios
崽崽长肉肉11 小时前
swift中的知识总结(一)
ios·swift
猫头虎12 小时前
GoLand 2025.3 最新变化:值得更新吗?
ide·windows·macos·pycharm·编辑器·intellij-idea·idea
2501_9151063214 小时前
HTTP 协议详解,HTTP 协议在真实运行环境中的表现差异
网络·网络协议·http·ios·小程序·uni-app·iphone
_可乐无糖16 小时前
内网穿透工具使用指南:ngrok 和 cpolar
安全·macos·云计算
柯南二号16 小时前
【大前端】【iOS】iOS 真实项目可落地目录结构方案
前端·ios
2501_9160074716 小时前
iOS与Android符号还原服务统一重构实践总结
android·ios·小程序·重构·uni-app·iphone·webview
不爱吃糖的程序媛17 小时前
鸿蒙PC命令行开发 macOS 上解决 pkg-config 命令未安装的问题
macos·华为·harmonyos
二流小码农18 小时前
鸿蒙开发:自定义一个圆形动画菜单
android·ios·harmonyos
淘气淘的宝宝18 小时前
MAC intel 迁移至M4后brew及iterm2配置
macos