如何从服务器下载文件时创建进度条

时间:2012-09-28 07:10:09

标签: iphone objective-c ios xcode cocoa-touch

如何在iPhone中从服务器下载文件时创建进度条

4 个答案:

答案 0 :(得分: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.

这可能对你有帮助。

答案 1 :(得分:0)

使用download helper class下载文件,它也显示进度条指示..

答案 2 :(得分:0)

您的UI的主线程中不应该有进度条。

最好的方法是创建另一个线程并将进度条放在那里。您可以向此线程发送一些更新信息,例如百分比,以便以您喜欢的速度更新。

完成下载后,线程可能会被杀死 - 或者将其保留在后台进行后续下载。

答案 3 :(得分: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){
}
}