swift - 从图像资产中获取数据
问题描述
我在资产目录中有一张图片,我需要UIImage
使用 scale 参数获取这张图片 ( )。只有一种UIImage(data: scale:)
方法,但我无法使用名称从资产中获取数据。我总是得到url = nil
。也许问题出在目录结构上!?
我的代码:
if let url = Bundle.main.url(forResource: "countdown_photo_1", withExtension: "jpg") {
return UIImage(data: try! Data(contentsOf: url), scale: scale)
}
我的目录结构:
我的 Contents.json:
{
"images" : [
{
"idiom" : "universal",
"filename" : "countdown_photo_1.jpg"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
解决方案
试试这个代码可能对你有用
guard let img = UIImage(named: "countdown_photo_1.jpg") else { return }
let data = img.jpegData(compressionQuality: 1)
推荐阅读
- flutter - 在 null 上调用了方法“findRenderObject”
- node.js - 如何检测 Node.js 中的无限循环?
- c - 使用 libCurl 的 EST 客户端的 simpleeenroll
- spring - 自动装配参数作为 bean 声明参数
- python-3.x - 从另一个 pyspark 数据帧中的一个 pyspark 数据帧中搜索字符串
- gatling - 编辑“gatling.conf”时无法更改加特林指标范围
- wordpress - 如何获得库存产品变化的最低或最高价格?
- python - 使用 Metatrader5 库使用 python 更改 metatrader5 中的止损,但没有任何反应
- javascript - 如何使用 JQuery 将文本中的主题标签转换为粗体?
- python-3.x - Python中的动态变量命名