【问题标题】:iOS Asihttprequest custom progress trackingiOS Asihttprequest 自定义进度跟踪
【发布时间】:2012-05-03 14:59:16
【问题描述】:

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

我试过了,但是不行!! 可变数据为空! 我使用带有 ARC 的 iOS 5。 感谢您的帮助!

更新

我用这段代码解决了问题,我已将委托设置为 self 并且一切正常。 只是我不明白是否以这种方式和使用 ARC 我需要清除委托

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

感谢大家的帮助!!

更新

-(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];
}

【问题讨论】:

  • 据我所知,当您决定手动保存数据时,您还必须通过检查已接收数据的长度来手动处理进度。至于你如何确定最终的总长度……不确定。
  • 感谢 Jacob,我认为 appendData 会逐步存储下载的数据。 -(void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data 被不断调用,并且 (NSData *)data 的大小如果我记录它会发生变化。但最后收到的数据为空

标签: ios asihttprequest


【解决方案1】:

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

请注意:您写的问题的标题是iOS Asihttprequest 自定义进度跟踪。如果您查看the ASIHTTPRequest documentation,您会看到一个标题,它准确描述了您所要求的内容 - Custom progress tracking

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

【讨论】:

  • 谢谢吉姆,我总是在寻求帮助之前检查文档。我问是因为我尝试实施但没有成功,总是返回下载错误。我要求了解是否有人已经成功使用了这个代表。谢谢
  • 一个大问题?!比如全球变暖或饥荒?
猜你喜欢
  • 1970-01-01
  • 2019-12-10
  • 2011-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多