Apple Watch 不显示任何图像

Apple watch not showing any images

我们在 Apple Watch 上无法显示图像时遇到问题,我们尝试了以下方法:

我们的图片位于 apple watchkit 扩展中,我们尝试将其放入缓存中:

let img = UIImage(named:"slider_r.png")
WKInterfaceDevice().addCachedImage(img!, name:"slider_r.png")

直接崩溃了...

然后我们尝试直接在apple watchkit app上添加图片:没有资源。 没有加载图像...

然后我们尝试将其放入 image.assets 中也没有用... 我们目前没有想法...

谢谢!

好的,我已经使用 WatchKit 大约一个月了 - 这是我所知道的:

  1. 将您的图像放入 Watch 应用程序的资产目录 会将其添加到 Watch 的捆绑包中。这将立即可用于使用 UIImage 的 .setImageNamed("name").
  2. 将您的图像放入 Watch Extension 的资产目录 会将其添加到扩展包中,驻留在 phone 上。现在,要将其添加到手表,您必须首先从扩展 中获取对图像 的引用,然后使用 WKInterfaceDevice().addCachedImage(image, name:).[=36 将其添加到设备的缓存中=]

查看有关向 Watch 提供图像的 Apple 文档 here。这是 setImage(_ image: UIImage?):

的文档

This method changes the image being displayed. Use this method to send images from your WatchKit extension to the WatchKit app. The image interface object is resized to accommodate the size of the newly specified image. If the image itself is too large for the device’s screen, the image is clipped.

这不是你想要的。您希望将图像显式添加到缓存中以便稍后使用。如果您如上所述使用 setImage,它会在您每次调用时将图像从 phone 传输到手表,如果您在多个位置使用相同的图像或者您非常频繁地使用它。您必须谨慎选择要在手表上缓存的确切图像。

使用我们现在知道的:

var img: UIImage = UIImage(named: "slider_r") // grabs the image from extension's bundle
WKInterfaceDevice().addCachedImage(img!, named: "slider_r") // adds to the Watch cache

myImageOutlet.setImageNamed("slider_r") // grabs the image from the Watch cache
  • 确保资产目录包含在扩展的目标中,以便图像在运行时正确放置在包中。
  • 不需要在图像名称中包含扩展名(例如“.png”)。