目前,我在屏幕上有一个图像,它每5秒与另一个图像交换一次,并使用动画来完成此操作。
同时,在屏幕上,我有用户可以拿起并拖动的对象(使用平移手势)。在动画的.5期间,如果我在对象周围移动,UI就会卡顿。例如,我有一个画笔,我拿起它在屏幕上移动。5秒计时器结束,背景图像更新。而这会在动画发生时更新画笔卡顿。我移动了加载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
发布于 2013-07-13 02:49:24
iPhone上的图像处理库并不神奇,它们确实需要花费CPU时间来实际解码图像。这很可能是您遇到的问题。调用UIImage imageNamed可能会缓存图像,但缓存总是可以刷新的,因此不会强制系统将图像保留在内存中。调用initWithData的代码是没有意义的,因为它仍然需要将PNG解压缩到内存中,而这正是导致速度减慢的原因。您可以做的是将图像渲染为解码的像素,然后将其保存到一个文件中。然后,对文件进行内存映射,并将映射后的内存封装在coregraphics图像中。这将避免可能导致速度减慢的“解码和渲染”步骤。但是,其他任何东西实际上可能都不会像您期望的那样。哦,你不应该在内存中保存解码的字节,因为图像数据通常非常大,它将占用设备内存中太多的空间。
https://stackoverflow.com/questions/17619193
复制相似问题