2012-09-28 55 views

回答

1

以下爲下載進度條顯示

進口「UIDownloadBar.h」

@interface yourClassViewCtr : UIViewController <uidownloadbardelegate ,UIAlertViewDelegate> { 
    UIDownloadBar *bar; 
    UILabel *lblForDisplay; 
    UIAlertView *alert; 
} 

代表法碼代碼

#pragma mark - UIDownloadBar Delegate Methods 

- (void)downloadBar:(UIDownloadBar *)downloadBar didFinishWithData:(NSData *)fileData suggestedFilename:(NSString *)filename { 
// NSLog(@"%@", filename); 
// NSLog(@"%@",fileData); 

    UIImage *img=[UIImage imageWithData:fileData]; 
    UIImageView *imgVctr=[[UIImageView alloc]initWithFrame:CGRectMake(40, 25, 200, 100)]; 
    imgVctr.image=img; 

    //store locally data into the resource folder. 
    [self.view addSubview:imgVctr]; 
    [alert dismissWithClickedButtonIndex:0 animated:YES]; 
} 

- (void)downloadBar:(UIDownloadBar *)downloadBar didFailWithError:(NSError *)error { 
    NSLog(@"%@", error); 
} 

- (void)downloadBarUpdated:(UIDownloadBar *)downloadBar { 
} 

For more detail visit following tutorial.

這可能對你有幫助。

0

你的UI的主線程中不應該有進度條。

最佳方法是創建另一個線程並將您的進度欄放在那裏。您可以發送此線程一些更新信息,如百分比,以便更新您喜歡的速度。

一旦你完成下載的線程可以被殺死 - 或留下來在後臺下載。

+0

你會如何安全地把視圖放在這樣的後臺線程上?爲什麼你不應該在主線程中更新它? –

+0

...因爲如果下載有問題,應用程序可能會被阻止 - 那麼UI會凍結 - 這不是最好的用戶體驗。但正如王子指出的那樣,這個目的可能是更直接的方法。無論如何,Apple/Android等強烈建議不要將這些代碼放在主UI線程中,這是由於解釋的UI阻塞問題。這是一個非常簡單的創建一個新的線程 - 只需要搜索http://www.codeproject.com/Tips/83317/BackgroundWorker-and-ProgressBar-demo – user387184

+2

你的答案看起來像你說的更新UI元素本身從後臺線程可能會變得很糟糕。並不是說你要在後臺線程上運行連接。當然,你應該這樣做,但是向主線程發出信號以在數據進來時更新進度條。我不認爲你的答案中會出現這種情況。 –

0
- (id)initWithFrame:(CGRect)frame 
{ 
self = [super initWithFrame:frame]; 
if (self) { 

    [self setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.8]]; 
    [self setAutoresizesSubviews:YES]; 

    self.downloadMessage=[[[UILabel alloc] initWithFrame:CGRectMake(40, 0, frame.size.width-80, frame.size.height/2)] autorelease]; 

    [self.downloadMessage setTextAlignment:UITextAlignmentCenter]; 
    [self.downloadMessage setFont:[UIFont boldSystemFontOfSize:11]]; 
    [self.downloadMessage setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin]; 
    [self.downloadMessage setText:@"Downloading Assets..."]; 
    [self.downloadMessage setBackgroundColor:[UIColor clearColor]]; 
    [self addSubview:self.downloadMessage]; 
    [self.downloadMessage setTextColor:[UIColor whiteColor]]; 

    float offset=(frame.size.width-200)/2; 
    self.progressiveView=[[[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault] autorelease]; 

    [self.progressiveView setFrame:CGRectMake(offset, frame.size.height/2, 200, frame.size.height/2)]; 
    [self.progressiveView setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin]; 
    [self addSubview:self.progressiveView]; 
} 
return self; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response{ 

expectedLength+=response.expectedContentLength; 
} 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 

@try{ 

int index=[self.urlConnections indexOfObject:connection]; 
progressValue+=((float)((float)data.length/(float)expectedLength))*0.9; 
[self.progressiveView setProgress:progressValue]; 
[[self.preCacheDatas objectAtIndex:index] appendData:data]; 
} 
@catch (NSException *exception){ 
} 
} 
相關問題