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)

}

相关推荐
hey you~8 分钟前
嘈杂环境ASR识别优化:降噪、VAD切分与Whisper模型微调完整教程(2026版)
macos·whisper·xcode
风雨_2 小时前
Mac M3 使用 OrbStack 全新安装运行 EasyConnect 完整教程
ios
Khalil三省3 小时前
Mac怎么显示桌面歌词?Apple Music和Spotify也能开
macos·开源·效率工具·音乐
sakiko_4 小时前
纯Appdelegate控制的老项目适配Xcode27(iOS27SDK)
macos·objective-c·cocoa
Coder_张三1 天前
iOS 26 适配
前端·ios·swift
用户38034165882971 天前
崩溃率从 0.3% 到 0.04%:我们做对的不是修 bug,是让每个崩溃只发生一次
ios·swift
2501_915921431 天前
详细教程:使用Perfdog工具测量iOS应用的功耗性能与优化方法
android·macos·ios·小程序·uni-app·cocoa·iphone
风雨_1 天前
RTK (Rust Token Killer) 安装、使用与卸载指南
ios
茶底世界之下1 天前
夜景暗部要提亮多少?用 Awaking 给雪夜街道留一点层次
ios