Search code examples
iosswiftxcodeapple-watchwatchos

Apple watch multiple Assets.xcassets folder purpose


What's the difference between the Assets.xcassets in

1) WatchKit App

2) WatchKit Extension

I'm not sure which folder should I place the image in.

I need to access the image (e.g. testImage.png) in both Interface.storyboard which resides in WatchKit App folder and in WatchKit Extension. In the controller I'm setting it using:

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

I've tried:

Option 1:

1) to place images in both WatchKit App and WatchKit Extension

Which will mean duplicate images, in two Assets.xcassets folder.

Option 2:

1) to place images in both WatchKit App only and change the target membership of the Assets.xcassets to support both WatchKit App and WatchKit Extension

Which approach is better? or are there any better ways?


Solution

  • This seems to be a holdover from watchOS 1 when the WatchKit Extension was actually running on the iPhone and sending information and resources to the WatchKit App running on the watch.

    There are some calls which will work only with the WatchKit App's Assets.xcassets folder such as setImageNamed: or 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

    Personally I would not place images in both folders as this will increase the size of your App. I tend to place images that will be set by Storyboard in the WatchKit App's folder and all the images that will change programmatically in the Extension.