2014-02-16 168 views
0

我在寻找一些建议,可能会导致我的IOS应用程序中的CPU出现问题。CPU运行在100%以上

该应用程序运行良好,过了一段时间后,CPU将保持高速运行。我使用UICollectionView来显示使用线程生成和编辑的水平缩略图。当我使用UICollectionView并滚动图像时,尖峰似乎发生。

我发布了一个问题的屏幕截图,下面的代码是我用来编辑图像的。

enter image description here

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; 
} 

这可能是另一段代码,是造成它,但我不知道?

+0

我已在editImage中添加。谢谢 – ORStudios

+0

请记住,我们正在寻找密集的部分,这不是它。深入一层,请显示'processImage:' –

回答

3

您没有提供足够的信息来帮助我们。

一般而言,当您将新的表格视图/集合视图单元格滚动到视图中时,您希望避免任何“繁重工作”。您需要一个方案,您可以在其中显示占位符图像,将处理排队到后台队列,并在完成后更新结果。

同样重要的是,您希望缓存结果,因此您只能处理每个图像一次。如果用户向下滚动,然后滚动回来,则应该在图像缓存中找到已处理的图像并使用这些图像。内存中缓存速度最快,但即使将处理后的图像缓存到磁盘,也可能会显着提高速度。

当你开始一堆后台处理时,你的CPU使用率变得非常高,这是非常正常的,并不一定是问题。您只需确保后台处理不会导致用户界面不稳定。假设您有一个很好的系统将图像处理排队到后台队列,并在准备就绪后缓存显示处理后的图像,则可能需要将处理队列中的优先级恢复到下一个较低优先级DISPATCH_QUEUE_PRIORITY_LOW。这会给用户交互带来更高的优先级,并减慢图像处理速度。

如果我没有记错的话,CPU使用最大值为100 x设备上的核心数。因此,如果您使用的是2核iPhone 4s,则最大CPU为200.