Apple watch 多个 Assets.xcassets 文件夹用途

Apple watch multiple Assets.xcassets folder purpose

中的Assets.xcassets有什么区别

1) WatchKit 应用程序

2) WatchKit 扩展

我不确定应该将图像放在哪个文件夹中。

我需要访问位于 WatchKit App 文件夹中的 Interface.storyboardWatchKit Extension 中的图像(例如 testImage.png)。在控制器中,我使用以下方式设置它:

let menuIcon: UIImage? = UIImage(named: menu.iconName)

我试过:

选项 1:

1) 在 WatchKit AppWatchKit Extension

中放置图像

这意味着重复图像,在两个 Assets.xcassets 文件夹中。

选项 2:

1) 仅在 WatchKit App 中放置图像并更改 Assets.xcassets 的目标成员以支持 WatchKit AppWatchKit Extension

哪种方法更好?或者有什么更好的方法吗?

这似乎是 watchOS 1 的遗留问题,当时 WatchKit 扩展实际上 运行 在 iPhone 上,并将信息和资源发送到手表上的 WatchKit 应用程序 运行。

有些调用仅适用于 WatchKit 应用程序的 Assets.xcassets 文件夹,例如 setImageNamed:setBackgroundImageNamed:

There are several ways to change the current image of an interface object:

  • Use the setImageNamed: or setBackgroundImageNamed: methods to assign an image that is already in the Watch app bundle.

  • Use the setImage:, setImageData:, setBackgroundImage:, or setBackgroundImageData: >methods to transfer image data from your WatchKit extension to your Watch app.

Specifying images by name is more efficient because only the name string must be transferred to your Watch app. watchOS searches your Watch app bundle for an image file with the name you specified. The most efficient way to specify images efficiently is to store them in your Watch app bundle and use the setImageNamed: or setBackgroundImageNamed: as appropriate to configure the corresponding object.

Images created in your WatchKit extension must be transferred to the Watch app before they can be used. For example, using the imageNamed: method in your extension loads the image from your WatchKit extension’s bundle, not from your Watch app’s bundle. You can then call the setImage: method, passing in the image object. The WatchKit extension automatically transfers the image to the Watch app for display. While this has some additional overhead compared to loading images directly from the Watch app bundle, It should not have a significant impact on either performance or battery life.

App Programming Guide for watchOS / Images

就我个人而言,我不会将图像放在两个文件夹中,因为这会增加您的应用程序的大小。我倾向于将故事板设置的图像放在 WatchKit 应用程序的文件夹中,并将所有将以编程方式更改的图像放在扩展中。