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)

}

相关推荐
择势12 小时前
基于声网 Agora RTM + RTC SDK 实现 iOS 语音聊天室 —— 常见问题汇总 & 解决方案手册
ios
择势13 小时前
基于声网 Agora RTM + RTC SDK 实现 iOS 语音聊天室(进阶封装)
ios
择势13 小时前
基于声网 Agora RTM + RTC SDK 实现 iOS 语音聊天室——从零到可跑的指南
ios
白玉cfc13 小时前
【iOS】底层原理:类的加载
ios·objective-c·xcode
天上路人14 小时前
AI 降噪不是“凭空复原语音”,而是在“已有语音信息”的基础上进行增强。
ide·人工智能·macos·语音识别·xcode
один but you14 小时前
【无标题】
macos·objective-c·cocoa
光电的一只菜鸡15 小时前
shell脚本开发技巧
开发语言·ios·swift
AI行业学习16 小时前
CC-Switch 下载、安装与使用全指南Windows+macOS+Linux【2026.5.28】
linux·windows·macos
2501_9160074716 小时前
iOS应用性能优化全面指南:从内存管理到工具使用
android·ios·性能优化·小程序·uni-app·iphone·webview
库奇噜啦呼17 小时前
【iOS】源码学习-类的加载
学习·ios·cocoa