我正在寻求一些建议,以了解可能导致我的 IOS 应用程序中的 CPU 出现问题的原因。
该应用程序将运行良好,然后在一段时间后 CPU 将达到峰值并保持高位。我使用 UICollectionView 来显示使用线程生成和编辑的水平缩略图。当我使用 UICollectionView 并滚动图像时,峰值似乎发生了。
我已经发布了有问题的仪器的屏幕截图,下面的代码是我用来编辑图像的代码。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage *imageWithEffect = [self editImage:[[self.activeEffectsArray objectAtIndex:index] objectAtIndex:0] ImageToEdit:self.thumbnailImage];
dispatch_async(dispatch_get_main_queue(), ^{
[imageViewWithEffect setImage:imageWithEffect];
[activityIndicator removeFromSuperview];
[[self.activeEffectsArray objectAtIndex:index] insertObject:imageWithEffect atIndex:2];
});
});
- (UIImage *)editImage:(NSString *)effectName ImageToEdit:(UIImage *)tmpImage {
EffectBigApple *effectBigApple = [[EffectBigApple alloc] init];
if([effectName isEqualToString:@"BigApple 1"]){
return [effectBigApple processImage:tmpImage filterType:@"BigApple 1" sliderValue:1 type:nil];
}
if([effectName isEqualToString:@"BigApple 2"]){
return [effectBigApple processImage:tmpImage filterType:@"BigApple 2" sliderValue:1 type:nil];
}
if([effectName isEqualToString:@"BigApple 3"]){
return [effectBigApple processImage:tmpImage filterType:@"BigApple 3" sliderValue:1 type:nil];
}
if([effectName isEqualToString:@"BigApple 4"]){
return [effectBigApple processImage:tmpImage filterType:@"BigApple 4" sliderValue:1 type:nil];
}
return nil;
}
可能是另一段代码导致了它,但我不确定?
最佳答案
您没有提供足够的信息让我们帮助您。
通常,当将新的 TableView / Collection View 单元格滚动到 View 中时,您希望避免任何“繁重的工作”。您需要一种显示占位符图像、将处理排队到后台队列并在完成时更新结果的方案。
同样重要的是,您希望缓存结果以便您只处理每张图像一次。如果用户向下滚动,然后向后滚动,您应该在图像缓存中找到已经处理过的图像并使用它们。内存缓存是最快的,但即使将处理后的图像缓存到磁盘也可能会显着提高速度。
启动一堆后台处理时,CPU 使用率非常高是很正常的,不一定是问题。您只需要确保后台处理不会使用户界面滞后。假设您有一个很好的系统可以将图像处理排队到后台队列并在图像准备好后缓存显示已处理的图像,您可能需要将处理队列的优先级调回下一个较低的优先级,DISPATCH_QUEUE_PRIORITY_LOW。这会给用户交互带来更高的偏好,并减慢图像处理速度。
如果我没记错的话,CPU 使用的最大值是设备内核数的 100 倍。因此,如果您使用的是 2 核 iPhone 4s,则最大 CPU 为 200。
关于ios - CPU 运行率超过 100%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21812422/