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)

}

相关推荐
肖永威1 小时前
macOS环境安装/卸载python实践笔记
笔记·python·macos
Haha_bj2 小时前
Flutter ——flutter_screenutil 屏幕适配
android·ios
Haha_bj3 小时前
Flutter ——device_info_plus详解
android·flutter·ios
雨中风华6 小时前
Linux, macOS系统实现远程目录访问(等同于windows平台xFsRedir软件的目录重定向)
linux·windows·macos
山水域7 小时前
SKAdNetwork 6.0 深度实战:多窗口转化值(Conversion Value)建模与数据分层架构
ios
JavinLu9 小时前
ios 配置了代理且使用 chls.pro/ssl 下载不了证书,无法弹出下载证书的提示问题
网络协议·ios·ssl
G311354227310 小时前
免费苹果 Plist 文件在线制作 iOS IPA 安装工具
ios
有趣的杰克10 小时前
开源|macOS 菜单栏 AI 启动器 GroAsk:⌥Space 一键直达 ChatGPT / Claude / Gemini
人工智能·macos·chatgpt
疯狂敲代码的老刘10 小时前
JDK 1.6到25 全版本网盘合集 (Windows + Mac + Linux)
java·linux·windows·macos·jdk
2401_8322981012 小时前
免费p12证书在线检测iOS苹果证书状态一键查询
ios