2

I have an app processing a couple of images using Quartz, and i wanted to have a UIProgressView that changes after each actions. (e.g. 0.0 0.2 0.4 0.6 0.8 1.0)

The problem is, it seems while my image is being processed the UI is completely locked, and the value only changes after all of the process is done (meaning it just gets to 1.0 without going through the sub-steps),

Did any of you ever encounter this ?

Pseudo:

for(uint i=0;i<5;i++){
    // Execute some Quartz based action here, such as CGContextDrawTiledImage etc...
    myProgress.progress = (i+1) * 0.2;          
}

So actually instead of the progress bar changing after each action, it only changes once at the end to 1.0. Would appreciate your feedback or experience or this.

Thank you
Shai.

Shai Mishali
  • 9,224
  • 4
  • 56
  • 83

1 Answers1

2

You'll need to update either your assets or your progress bar in a separate thread so that the two can update in parallel.

Have a look at [NSThread detachNewThreadSelector:selector toTarget:target withObject:object];

Make your progress a member variable

 [NSThread detachNewThreadSelector:@selector(updateFilterProgress) toTarget:self withObject:nil];

 prgLoader.progress = x;


- (void) updateFilterProgress{

    NSAutoreleasePool *pool = [NSAutoreleasePool new];

    while ([prgLoader.progress floatValue] < 1.0f)    //Keep this thread alive till done loading. You might want to include a way to escape (a BOOL flag)
    {
        GTMLoggerInfo(@"Updating progress to %f", [progress floatValue]);
        prgLoader.progress = [progress floatValue];
    }
    [pool release];

}
James Webster
  • 31,873
  • 11
  • 70
  • 114
  • Just to make sure, lets say ill put the myProgress.progress line in a different selector, and execute it in a different thread? Is that what you offer? I'll try :) sounds good. – Shai Mishali Sep 02 '11 at 11:25
  • Hey, i've tried the following with no success, am i doing anything wrong? Also attached the log: http://pastebin.com/GfCqmq1s – Shai Mishali Sep 02 '11 at 11:35
  • Nevermind , just added NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; and it fixed the issue :) – Shai Mishali Sep 02 '11 at 11:38
  • Oh i see, so its best to have a single thread once and kill it at the end instead of running and killing a thread for every update... makes sense :) – Shai Mishali Sep 02 '11 at 11:44
  • Not necessarily, that's just the way I would have done it. It certainly may not be the best and it may be subject to problems that arise with threading. Consider adding to it with @synchronized or other methods to make it threadsafe – James Webster Sep 02 '11 at 11:45
  • I'm not really sure whats @synchronized, any link i could read perhaps? Much appreciated :) – Shai Mishali Sep 02 '11 at 11:49
  • This is the first I found: http://softpixel.com/~cwright/programming/threads/threads.cocoa.php Your buzzwords are: multithreading, synchronized, mutex, threadsafe etc if you want to search yourself – James Webster Sep 02 '11 at 11:54