如何在 iPhone 中从服务器下载文件时创建进度条。
问问题
4380 次
4 回答
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 {
}
这可能对您有所帮助。
于 2012-09-28T07:13:05.227 回答
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){
}
}
于 2012-09-28T07:35:46.220 回答
0
使用下载助手类下载文件,它也显示进度条指示..
于 2012-09-28T07:11:55.880 回答
0
您的 UI 的主线程中不应该有进度条。
最好的方法是创建另一个线程并将进度条放在那里。您可以向此线程发送一些更新信息,例如百分比,以便它以您喜欢的速度更新。
完成下载后,线程可以被杀死 - 或者将其留在后台进行后续下载。
于 2012-09-28T07:13:19.503 回答