I am developing Apple watch app, Displaying data using WKInterfaceTable, each row has a animated image (80frames, 10sec duration)
I want to animated this image every 10 second, but there is a problem, this causes the Watch very lag when the image finish animating and started to animate again.
I've tried to remove the animate image to test if the lag issue caused by image, and the result is YES.
I searched for solutions that to cache image on Apple Watch, I've done the following code:
First, lazy load image from iPhone, create an animated UIImage
- (UIImage *)countDownImage
{
if (_countDownImage == nil) {
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i=0; i<=79; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"counteight-%i", i]];
[array addObject:image];
}
_countDownImage = [UIImage animatedImageWithImages:array duration:10.0f];
}
return _countDownImage;
}
Second, in my WKInterfaceController that contains WKInterfaceTable
Add cached image to cache using WKInterfaceDevice
if ([[WKInterfaceDevice currentDevice].cachedImages objectForKey:@"countImage"] == nil) {
[[WKInterfaceDevice currentDevice] addCachedImage:self.countDownImage name:@"countImage"];
}
Finally, in my RowController, use WKInterfaceImage's setImageNamed to set the image I've previously cached
[self.animateImage setImageNamed:@"countImage"];
[self.animateImage startAnimatingWithImagesInRange:NSMakeRange(0, 79) duration:10 repeatCount:0];
It works and the image was cached (Checked by:
[[WKInterfaceDevice currentDevice].cachedImages objectForKey:@"countImage"]
)
But it is still very laggy on real Apple Watch device, even lagger than no cache image setted.
I don't know where I'm doing wrong, could somebody give me some advice that how to cache animated image on Apple Watch properly?
Thanks!