Search code examples
iphoneuiimageviewinstrumentsuicollectionview

Instruments Optimization UIImageView


I'm loading a bunch of UIImages (that are local and preloaded with the app) with this code:

NSString*plistPath = [[NSBundle mainBundle] pathForResource:@"images" ofType:@"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:plistPath];
NSMutableArray *finalArray = [[NSMutableArray alloc] initWithCapacity:array.count];
for(int i=0;i<array.count; i++) {
    [finalArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%@",[array objectAtIndex:i]]]];
}

then later in the collectionView:

cell.cellImage.image = [finalArray objectAtIndex:indexPath.row];

There's a huge lag on the presentation of that collectionView. I've captured that lag with the following Instruments Screen Shots:

enter image description here

There's a major disconnect between that lag (experienced by user and shown in the graph) and the actual call Tree below, which shows no delay. I'm loading 500k jpg's in each cell of the collectionView. Is this Instruments analysis basically saying there's nothing I can do in code and this is a system library delay?

EDIT: My question is no longer about this specific use case with my collectionView. It's about Instruments in general. If I have the options selected that I do in the screenshot and the app shows significant lag, but the time profiler doesn't, is there anything I can do to in my code to increase the speed? I'm guess I'm asking more generally about time profiler, settings, and their results. If all the lag is attributed to lower level code or system frameworks, can I do anything about it?


Solution

  • You have the Hide System Libraries checkbox enabled in the right settings panel so it will hide any calls to methods which you did not define. So for instance, if you make a bunch of calls to imageNamed: and you have this checkbox selected, you will not see the time it takes the system to decompress and load these images.

    So technically all the "lag"/slowness is in the system frameworks, but it is in your control to change how you use them. And you also can monitor which operations in system frameworks are most expensive time-wise.

    Unchecking this box is the solution:

    Uncheck this box