目前我在屏幕上有一个图像,每隔5秒换一张图像,并使用动画来完成。
同时在屏幕上我有用户可以拾取和拖动的对象(使用平移手势)。在动画的.5持续时间内,如果我在对象周围移动,则UI会断断续续。例如,我有一个刷子,我拿起并在屏幕上移动。 5秒计时器结束,背景图像更新。这会在动画发生时更新画笔卡顿。我移动了Image加载UI线程并强制它使用NSData加载。
在更改图像的动画正在运行时,有没有办法可以防止这种口吃。这是我交换图像的方式。
// Dispatch to the queue, and do not wait for it to complete
// Grab image in background thread in order to not block UI as much as possible
dispatch_async(imageGrabbingQueue, ^{
curPos++;
if (curPos> (self.values.count - 1)) curPos= 0;
NSDictionary *curValue = self.values[curPos];
NSString *imageName = curValue [KEY_IMAGE_NAME];
// This may cause lazy loading later and stutter UI, convert to DataObject and force it into memory for faster processing
UIImage *imageHolder = [UIImage imageNamed:imageName];
// Load the image into NSData and recreate the image with the data.
NSData *imageData = UIImagePNGRepresentation(imageHolder);
UIImage *newImage = [[UIImage alloc] initWithData:imageData];
dispatch_async(dispatch_get_main_queue(), ^{
[UIView transitionWithView:self.view duration:.5 options:UIViewAnimationOptionTransitionCrossDissolve|UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionAllowAnimatedContent
animations:^{
[self.image setImage:newImage ];
// Temp clause to show ad logo
if (curPos != 0) [self.imagePromotion setAlpha:1.0];
else [self.imagePromotion setAlpha:0];
}
completion:nil];
});
});
谢谢, DMAN
答案 0 :(得分:0)
iPhone上的图像处理库并不神奇,它们需要花费CPU时间来实际解码图像。这很可能是你遇到的。调用UIImage imageNamed可能会缓存图像,但是总是可以刷新缓存,这样就不会强制系统将图像保留在内存中。调用initWithData的代码是没有意义的,因为它仍然必须将PNG解压缩到内存中,这是导致速度减慢的部分。你可以做的是将图像渲染为解码像素,然后将其保存到文件中。然后,内存映射文件并将映射的内存包装在coregraphics映像中。这将避免可能导致减速的“解码和渲染”步骤。但是,其他任何事情可能实际上并不符合您的期望。哦,你不应该把解码后的字节保存在内存中,因为图像数据通常很大,以至于在设备内存中会占用太多空间。