2

I am currently building an application that generates images and stores them in an NSMutableArray, which then used in a UINavigation (Cell.imageView.image). I need to be able to handle up to 2000 images without causing lag in my application.

Currently how I set this generating is by calling the generating method when cellForRowAtIndexPath is accessed. Which seems to cause a 4-5 second lag before the next navigation is called.

fortunately after those 4-5 seconds the generating is done and there is no issues.

In the world of iProducts waiting 4-5 seconds isn't really an option. I am wondering what my options are for generating these images in the background. I tried using threads [self performSelectorInBackground:@selector(presetSnapshots) withObject:nil]; but that only gave me issues about vectors for some reason.

Heres the generating code:

-(void)presetSnapshots{
    //NSAutoreleasePool* autoReleasePool = [[NSAutoreleasePool alloc] init];
    for (int i = 0; i < [selectedPresets count]; ++i){
        GraphInfo* graphInfo = [selectedPresets objectAtIndex:i];
        graphInfo.snapshot = [avc takePictureOfGraphInfo:graphInfo PreserveCurrentGraph:false];
        [graphInfo.snapshot retain];
    }
    //[autoReleasePool drain];
    presetSnapshotFinished = YES;

}

inside - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { is

   if (presetSnapshotFinished == NO){
        [self presetSnapshots];
        //[self performSelectorInBackground:@selector(presetSnapshots) withObject:nil];

    }

    cell.imageView.image = [[selectedPresets objectAtIndex:indexPath.row] snapshot];

Edit:

I also rather not use coreData for this. The images are 23x23 and coming out to about 7kb. So its about 6MB being use at any giving time in memory.

John Riselvato
  • 12,854
  • 5
  • 62
  • 89

1 Answers1

1

You can use Grand Central Dispatch (GCD) to fire up [self presetSnapshots]

dispatch_queue_t working_queue = dispatch_queue_create("com.yourcompany.image_processing", NULL);
dispatch_queue_t high = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,NULL);
dispatch_set_target_queue(working_queue,high);
dispatch_queue_t main_queue = dispatch_get_main_queue();

dispatch_async(working_queue,^{
    if (presetSnapshotFinished == NO){
       [self presetSnapshots];
    }
    dispatch_async(main_queue,^{
        cell.imageView.image = [[selectedPresets objectAtIndex:indexPath.row] snapshot];
    });
});
dom
  • 11,894
  • 10
  • 51
  • 74
  • I'll read up more on this. But I tried out your code and got a BAD_ACCESS on stl_constuct.h specifically line 107 {__pointer->~_Tp(); } which looking at the stack, its an issue with my cleanup method. I'll figure out why the GCD and my clean up is conflicting. – John Riselvato Feb 24 '12 at 16:01
  • 1
    I guess … I'm using this snippet in most cases for background processing without any problems – dom Feb 24 '12 at 16:04