我正在开发 Apple Watch 应用程序,使用 WKInterfaceTable 显示数据,每一行都有一个动画图像(80 帧,10 秒持续时间)
我想每 10 秒为这张图片制作一次动画,但是有一个问题,当图像完成动画并再次开始动画时,这会导致 Watch 非常滞后。
我试图删除动画图像以测试是否由图像引起的滞后问题,结果是肯定的。
我搜索了在 Apple Watch 上缓存图像的解决方案,我完成了以下代码:
首先,从 iPhone 懒加载图片,创建动画 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;
}
其次,在我的WKInterfaceController中包含WKInterfaceTable
使用WKInterfaceDevice将缓存的图像添加到缓存
if ([[WKInterfaceDevice currentDevice].cachedImages objectForKey:@"countImage"] == nil) {
[[WKInterfaceDevice currentDevice] addCachedImage:self.countDownImage name:@"countImage"];
}
最后,在我的RowController中,使用WKInterfaceImage的setImageNamed 来设置我之前缓存的图像
[self.animateImage setImageNamed:@"countImage"];
[self.animateImage startAnimatingWithImagesInRange:NSMakeRange(0, 79) duration:10 repeatCount:0];
它可以工作并且图像已被缓存(通过以下方式检查:
[[WKInterfaceDevice currentDevice].cachedImages objectForKey:@"countImage"]
)
但在真正的 Apple Watch 设备上仍然非常滞后,甚至比没有设置缓存图像还要滞后。
我不知道我在哪里做错了,有人能给我一些建议,告诉我如何在 Apple Watch 上正确缓存动画图像吗?
谢谢!