datinc
datinc

Reputation: 3552

WKInterfaceDevice caching optimization

I am trying to cache rendered animations to the apple watch (these are generated at run time). I have saved the frames of each animation as JPEG @1x with compression of 0.1. The sum of all the frames is less then 1.2 MB. I clear the cache before I start caching. However only about half the animations are cached. The documentation says that the cache is 5MB. What am I doing wrong?

Upvotes: 0

Views: 511

Answers (2)

Matt
Matt

Reputation: 693

I just discovered that if you use this line of code:

[self.image setImageNamed:@"number"]

Your images should be named:

number1.png
number2.png
number3.png
number4.png

I was running into a similar error when I had my images named:

number001.png
number002.png
number003.png
number004.png

Upvotes: 0

Mike Swanson
Mike Swanson

Reputation: 3347

If you want to send image data to the Watch programmatically (i.e. not at compile time), WKInterfaceDevice provides two methods:

  1. addCachedImage:name: accepts a UIImage, encodes it as PNG image data, and transmits it to the cache. So, if you create a UIImage from JPEG data, you are actually decoding the JPEG data into an image, then re-encoding it as PNG before it's sent to the cache (thereby negating the effects of JPEG-encoding in the first place).
  2. addCachedImageWithData:name: accepts NSData and transmits the unaltered data directly to the cache. So, if you encode your image to NSData using UIImageJpegRepresentation and pass it to this method, you'll transmit and store less in the cache. I use this technique for all of my images, unless I need the benefits of a PNG image; in that case, I actually encode my own NSData using UIImagePngRepresentation and send it using this method.

For debugging purposes, it's helpful to use the [[WKInterfaceDevice currentDevice] cachedImages] dictionary to find the size of the cached image data. The dictionary returns a NSNumber with the size (in bytes) of the cache entry.

Upvotes: 2

Related Questions