简体   繁体   English

Xcode资产目录问题

[英]Xcode Asset Catalog issues

I just started using Asset Catalogs in Xcode, so had few questions about better organization of catalog. 我刚刚开始在Xcode中使用资产目录,因此对更好地组织目录没有什么疑问。

I have over 100 button images, each image has 5 versions(@1x, @2x, @3x, @1x iPad, @2x iPad). 我有100多个按钮图像,每个图像都有5个版本(@ 1x,@ 2x,@ 3x,@ 1x iPad,@ 2x iPad)。

  1. One way is to create 100 image sets in the catalog file. 一种方法是在目录文件中创建100个图像集。 However, that would be too clumsy. 但是,那太笨拙了。 Is it possible to group related image sets together? 是否可以将相关图像集组合在一起?

  2. Is there a better way of importing 500 images than manual drag and drop? 有比手动拖放更好的导入500张图像的方法吗?

Name your image files using the @x~device format, such as: 使用@x~device格式命名图像文件,例如:

btn1@1x~iphone.png
btn1@2x~iphone.png
btn1@3x~iphone.png
btn1@1x~ipad.png
btn1@2x~ipad.png

You can then add them as individual files in your project, putting them in groups / folders as desired: 然后,您可以将它们添加为项目中的单个文件,并根据需要将它们放在组/文件夹中:

在此处输入图片说明

Now, you can set a button (or image view, etc) image via code: 现在,您可以通过代码设置按钮(或图像视图等)图像:

if let img = UIImage(named: "btn1") {
    button1.image = img
}

and iOS / UIKit will automatically use the correct image for the device. 并且iOS / UIKit将自动为设备使用正确的图像。

Or, you can set the image in Storyboard / IB. 或者,您可以在情节提要/ IB中设置图像。 Note that when you select the image from the drop-down list in Attributes Inspector pane, it will show as: 请注意,当您从“属性检查器”窗格的下拉列表中选择图像时,它将显示为:

btn1.png

however, at run-time -- or, if you use Preview or View as: to change the device in Xcode -- the correct image will be used. 但是,在运行时-或者,如果您使用“预览”或“ View as:来更改Xcode中的设备,则会使用正确的图像。

Give it a try with these images: 试试看这些图像:

在此处输入图片说明 在此处输入图片说明 在此处输入图片说明

在此处输入图片说明 在此处输入图片说明

The @1x images are 128 x 128 , the @2x images are 256 x 256 and the @3x image is 384 x 384 . @1x图像是128 x 128@2x图像是256 x 256@3x图像是384 x 384

Note: You can also use Asset Catalog without having to drag the files one-by-one. 注意:可以使用资产目录,而不必一个一个地拖动文件。 If you have the set of 5 files names as above, open an Asset Catalog in Xcode and drag-and-drop all 5 files into the Catalog. 如果您具有上述5个文件名,请在Xcode中打开一个资源目录,然后将所有5个文件拖放到目录中。 Xcode will automatically configure them: Xcode将自动配置它们:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM