问题描述
是否可以将除PNG文件以外的资产添加到Xcode资产目录中?
Is it possible to add assets other than PNG files to an Xcode Asset Catalog?
当我将JPEG文件拖到资产目录中时,UI不会接受它们.
When I drag JPEG files into an Asset Catalog they aren't accepted by the UI.
推荐答案
您可以通过手动编辑资产的JSON表示来添加非PNG资产.最简单的方法是复制现有资产并对其进行修改:
You can add non-PNG assets by editing the JSON representation of the asset manually. The easiest way is to copy an existing asset and modify it:
- 右键单击现有资产,然后选择在Finder中显示
- 复制并粘贴现有的
.imageset
项目并重命名,例如my_image.imageset
- 双击新的
.imageset
- 删除文件夹中的所有现有图像
- 复制JPEG文件
- 编辑
Contents.json
文件,用您的JPEG文件名替换filename
键的值
- Right click on an existing asset and choose Show in Finder
- Copy and paste the existing
.imageset
item and rename it, e.g.my_image.imageset
- Double-click the new
.imageset
- Delete any existing images in the folder
- Copy in your JPEG files
- Edit the
Contents.json
file, replacing the values for thefilename
key with your JPEG filenames
您的Contents.json
看起来像这样:
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x",
"filename" : "my_image.jpg"
},
{
"idiom" : "universal",
"scale" : "2x",
"filename" : "[email protected]"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
请务必按名称引用图片,不带扩展名:
Be sure to refer to your image by name, without extension:
[UIImage imageNamed:@"my_image"]
此方法适用于GIF和其他资产,因为它们是在构建时被复制到App的主捆绑包中的.值得注意的是,复制到捆绑包中的图像最终以png扩展名结尾,但是它们仍然可以正确加载.
This approach will work for GIFs and other assets as they are just copied into the App's main bundle at build time. It is worth noting that the images end up with a png extension when copied to the bundle, but they still load correctly.
这篇关于如何在Xcode中将JPEG添加到资产目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!