问题
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.
回答1:
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];
}
来源:https://stackoverflow.com/questions/7282632/ios-setting-progress-bar-value-while-images-are-being-processed