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)

}

相关推荐
Karl_fang24 分钟前
🥳 再也不用拿起手机抄验证码!我做了一个 Android → Mac 的「验证码接力」工具
前端·macos·ai编程
二流小码农2 小时前
鸿蒙开发:router组件升级,彻底告别手动配置
android·ios·harmonyos
茶底世界之下8 小时前
雾中山路的冷光层次:用 Awaking 留住红色背影
ios·app·产品
Digitally10 小时前
iPhone 如何添加日历?
ios·iphone
Gracker1 天前
100 Years Life:把体检、睡眠和运动放进同一份健康档案
ios
csdn_aspnet1 天前
macOS 10 、Docker 玩轉 MySQL
运维·mysql·macos·docker·容器·mac
秋雨梧桐叶落莳1 天前
【iOS】Block底层详解
macos·ios·cocoa
茶底世界之下1 天前
舞台灯下的萨克斯:用 Awaking 让铜管从蓝幕前亮起来
ios·app·产品
传奇开心果编程1 天前
【Flutter踩坑排雷】第28课:平台目录缺失与修复坑|`flutter create` 的“重建”与“覆盖”边界
android·flutter·ios·移动开发·跨平台·dart·声明式ui
3095566661 天前
股票接口返回 404 与空数组的排查笔记:证书状态与级别参数
java·笔记·python·ios·iphone·股票数据api·股票数据接口