iOS setting Progress bar value while images are being processed

喜夏-厌秋 提交于 2019-12-12 19:23:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!