2012-01-18 37 views
0

我正在下載一些圖片並創建了一個進度條。我在NSURLConnection中使用異步模式,並同時啓動約15張圖片下載。在開始的過程中,我打電話didStartLoadImages,並且屏幕上的條已成功設置爲0寬度。從NSURLConnection委託調用後,幀大小不會直接更新?

現在問題開始,當其中一個圖像完成後,它會調用didLoadImageWithTotalPercentCompleted:並用當前百分比更新條形圖。它完美的工作和日誌寫得很好更新幀:20%等。但用戶界面不更新UNTILL所有的圖像都完成?

我只是注意到,即使它是異步的主線程被阻止?

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而不是int,對於所有數字,將它們寫爲100.0f而不是100,以強制C將它們視爲浮點數而不是整數。

基本上,在C中,如果你做1/2它不給你0.5,它會給你0,因爲它使用整數數學,2不會進入1整數次。因此,在計算百分比時,您確實需要使用浮點數,因爲(1/100)* x將始終爲零,但(1.0f/100.0f)* x將正常工作。

+0

你救了我的一天!當我從int更改爲'didLoadImageWithTotalPercentCompleted:(int)percent'中的float時,現在可以工作。我永遠不會看到那個錯誤。謝謝! – David 2012-01-18 14:11:06

相關問題