2012-01-18 6 views
0

私はいくつかの写真をダウンロードしており、進行状況バーを作成しました。 NSURLConnectionで非同期モードを使用しており、同時に約15枚のダウンロードを開始します。開始時に私はdidStartLoadImagesと呼び出し、バーは画面上で0幅に設定されます。フレームサイズはNSURLConnectionデリゲートから呼び出された直後に更新されませんか?

問題が始まると、イメージの1つが完成するとdidLoadImageWithTotalPercentCompleted:がコールされ、現在のパーセントでバーが更新されます。それは完璧に動作し、ログはうまく書いています。フレームを更新する:20%など。しかし、ユーザーインターフェイスは更新されず、すべての画像が完成していますか?

私は、メインスレッドが非同期であってもブロックされていることに気付きましたか?

Connection.m

-(void)loadImage:(NSString*)imageName numberOfImagesInSecquence:(int)nrOfImages { 
    NSString *url = [NSString stringWithFormat:@"https://xxx.xxx.xxx.xxx/~%@/files/%@",site,imageName]; 

    /* Send URL */ 
    NSURL *urlToSend = [[NSURL alloc] initWithString:url]; 
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:urlToSend]; 
    theConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES]; 

    self.receivedData = [NSMutableData data]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    connectionIsLoading = NO; 
    [[self delegate] didLoadImagesWithProcent:((float)1 - ((float)[imageArray count]/(float)nrOfImages)) *(float)100]; 
} 

MainViewController.m

-(void)didStartLoadImages { 
    /* Sets progress bar to 0% */ 
    [progressBar setBarWithPercent:0]; 
} 


-(void)didLoadImageWithTotalPercentCompleted:(int)percent { 
    if (percent == 100) { 
     /* Done */ 
    } else { 
     [progressBar setBarWithPercent:percent]; 
    } 
} 

ProgressBar.m

-(void)setBarWithPercent:(float)percent { 
    int maxSizeOfBar = 411; 

    [UIView beginAnimations:@"in" context:NULL]; 
    [UIView setAnimationDuration:0.2]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 

    CGRect frame = bar.frame; 
    frame.size.width = maxSizeOfBar * (percent/100) + 10; 
    bar.frame = frame; 
    NSLog(@"Updating frame to: %i",percent); 

    [UIView commitAnimations]; 

} 

答えて

1

これは整数数学切り捨てに問題があってもよいです。百分率の値を整数ではなくfloatまたはCGFloatに変更し、すべての数値に対して、100ではなく100.0fのように書くと、Cでは整数ではなく浮動小数点として扱われます。

基本的に、Cで1/2を実行すると0.5にはならず、整数計算を使用するため0になり、2は整数倍になりません。だからパーセンテージを計算するとき、(1/100)* xは常にゼロになるが、(1.0f/100.0f)* xは正しく働くので、実際には浮動小数点数を使用したい。

+0

あなたは私の日を救った!私がintからfloatに 'didLoadImageWithTotalPercentCompleted:(int)percent'で変更したときに、今すぐ完成します。私は間違いを見たことがないだろう。ありがとう! – David

関連する問題