Question

I am looking for some advice on what might be causing a problem with the CPU in my IOS application.

The app will run great and then after a while the CPU will spike and stay high. I use a UICollectionView to display horizontal thumbnails that are generated and edited using threads. The spike seems to happen when I use the UICollectionView and scroll the images.

I have posted a screenshot of instruments with the issue and the code below is what I use to edit the images.

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

It may be another section of code that is causing it but I am not sure?

Was it helpful?

Solution

You're not providing enough info for us to help you.

In general you want to avoid any "heavy lifting" when scrolling a new table view/collection view cell into view. You want a scheme where you display a placeholder image, queue the processing to a background queue, and update the results when they are complete.

Equally importantly, you want to cache the results so you only process each image once. If the user scrolls down, then scrolls back, you should find the already-procesed images in your image cache and use those. In-memory caching is fastest, but even caching the processed images to disk will likely make for a dramatic speed improvement.

Having your CPU use go very high when you start a bunch of background processing is quite normal, and not necessarily a problem. You just need to make sure that the background processing doesn't make the user interface laggy. Assuming you have a good system for queuing your image processing to a background queue and caching displaying the processed images once they are ready, you might need to dial back the priority on your processing queue to the next lower priority, DISPATCH_QUEUE_PRIORITY_LOW. That will give higher preference to user interaction, and slow down the image processing.

If I remember correctly the CPU use maxes out at 100 x the number of cores on your device. So if you are using a 2-core iPhone 4s, max CPU is 200.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top