开发者

UIProgressView not displaying progress accurately

I am using UIProgresView for displaying downloading and in label the percentage of content downloads.The function is executing every time. When I displaying the progress value it is showing properly in the console. But it is not displ开发者_StackOverflow中文版aying on the screen, It is display at only 0% and 100%. I am using ASIHTTP framework too.

Please help in that.

CODE from comment:

for (float i=0; i< [topicNew count]; i++) 
{ 
   NSDictionary *new= [topicNew objectAtIndex:i]; 
   NSString *imageName = [[[NSString alloc] initWithFormat:@"%@.%@.%@.png", appDelegate.subject,topicNamed,[new objectForKey:kWordTitleKey]] autorelease]; 
   NSString *imagePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:imageName]; 
   NSData *imageData = [self ParsingImagePath:[new objectForKey:kWordImagePathKey]]; 
   [progressView setProgress:i/[topicNew count]]; 
   [lblpercent setText:[[NSString stringWithFormat:@"%.0f",i/[topicNew count]] stringByAppendingString:@"%"]];

... More code here...


It looks like you are threadlocking your main thread. Meaning, you are updating the progressView but the mainthread never gets out of your for loop to display the updated change until the for loop is finished, which by then looks like your progress is set to 100%.

You need to either use a timer like (NSTimer) or some kind of background thread that processes your image files. Then you need to call your instance of UIProgressView from the background thread when you want to update your progress (end of the for loop for example)

float p = i/[topicNew count];
[progressView performSelectorOnMainThread:@selector(setProgress:) 
   withObject:[NSNumber numberWithFloat:p]
   waitUntilDone:NO];

and pass in your updated progress to the progressView. Make sure you are not calling the progressView from the background thread directly, as UIKit is not thread safe. Only call it from your main thread, or using the performSelectorOnMainThread:withObject:waitUntilDOne: method.

Hopefully that gets you in the right directly

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜