首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS Asihttprequest自定义进度跟踪

iOS Asihttprequest自定义进度跟踪
EN

Stack Overflow用户
提问于 2012-05-03 14:59:16
回答 1查看 2.5K关注 0票数 0

我用AsiHttpRequest显示下载的进度,比如: 300 kb /5 Mb,并不断更新第一个值。我知道如果我使用-(ASIHTTPRequest*)请求didReceiveData:(NSData *)数据,我需要自己管理nsdata对象并自己编写下载的文件。

我试过了,但没用!!可变数据为空!我在ARC中使用iOS 5。谢谢你的帮助!

更新

我用这段代码解决了问题,我已经将委托设置为self,并且每件事情都能正常工作。只是我不明白,如果用这种方式和ARC,我需要清除代表

代码语言:javascript
复制
- (void)dealloc
{
   [request clearDelegatesAndCancel];
}

感谢大家的帮助!

更新

代码语言:javascript
复制
-(void)downloadFile: (NSString*) file {
           NSString* urlFilePdf = [NSString stringWithFormat:@"http://www.mywebsite.com/%@",file];

            myFileName = file;

            NSURL *url = [NSURL URLWithString:urlFilePdf];

            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *filePath = [documentsDirectory stringByAppendingPathComponent:file];


            progressAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DOWNLOADFILE",nil) message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];

            progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(30.0f, 80.0f, 225.0f, 90.0f)];
            [progressAlert addSubview:progressView];
            [progressView setProgressViewStyle: UIProgressViewStyleDefault];

            lblTotal = [[UILabel alloc] initWithFrame:CGRectMake(5, 50, 273, 20)];
            [lblTotal setTextAlignment:UITextAlignmentCenter];
            [lblTotal setFont:[UIFont fontWithName:@"HelveticaNeue-Medium" size:14.0]];
            [lblTotal setTextColor:[UIColor whiteColor]];
            [lblTotal setBackgroundColor:[UIColor clearColor]];
            [progressAlert addSubview:lblTotal];

            requestFile = [ASIHTTPRequest requestWithURL:url];
            [requestFile setDelegate:self];
            [requestFile setDownloadDestinationPath:filePath];
            [requestFile setDownloadProgressDelegate:self];
            [requestFile setShowAccurateProgress:YES];    
            [progressAlert show];

            [requestFile startAsynchronous];

}

- (void)request:(ASIHTTPRequest *)request incrementDownloadSizeBy:(long long)newLength
{
    NSLog(@"%@",[NSString stringWithFormat:@"incrementDownloadSizeBy %quKb", newLength/1024]);
}

- (void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes
{
    NSLog(@"%@",[NSString stringWithFormat:@"didReceiveBytes %quKb", bytes/1024]);
    currentLength += bytes;
    [lblTotal setText:[NSString    stringWithFormat:@"%quKb/%@",currentLength/1024,self.TotalFileDimension]];
}

- (void)setProgress:(float)newProgress {
    NSLog(@"%f",newProgress);
    progressView.progress = newProgress;
}


-(void) requestFinished:(ASIHTTPRequest *)request
{
    // code ...
}

-(void) requestFailed:(ASIHTTPRequest *)request
{
    // code ...
}

- (void)dealloc
{
   [request clearDelegatesAndCancel];
}
EN

回答 1

Stack Overflow用户

发布于 2012-05-03 18:08:47

您应该实现request:incrementDownloadSizeBy:方法。

请注意:您所写问题的标题是iOS Asihttprequest自定义进度跟踪。如果您查看ASIHTTPRequest文档,您将看到一个标题,它准确地描述了您所要求的- 自定义进度跟踪

如果你不看这个,那是个大问题。在向人们寻求帮助之前,你应该始终检查文档。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10434021

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档