iPhone 的下载进度怎么弄?怎么得到下载请求的下载进度

解决方案 »

  1.   

    在响应里面得到文件的总大小,然后根据每次下载下来的data的大小与总大小的比例,设置进度条
      

  2.   


    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    //判断接受数据是否正确
    NSHTTPURLResponse *res=(NSHTTPURLResponse *)response;
    ringht = [res statusCode];
    //if (ringht == 206) {
    // //如果正确打开输出流
    //
    // NSString *openfile = [[NSString alloc] initWithFormat:@"%@%d.am", controller.Myfilenmame, controller.count];
    //     self.filestream=[NSOutputStream outputStreamToFileAtPath:[self FilePathByName:openfile] append:YES];
    // [self.filestream open];
    // }
    }//接收数据后回调
    - (void)connection:(NSURLConnection *)connection
    didReceiveData:(NSData *)data {
    if (ringht == 206) {
    NSString *openfile = [[NSString alloc] initWithFormat:@"%@%d.tmp", controller.Myfilenmame, controller.count];
    self.filestream=[NSOutputStream outputStreamToFileAtPath:[self FilePathByName:openfile] append:YES];
    [self.filestream open];

    if (controller.Sueper) {
    [self.filestream close];
    [self.connection cancel];
    }
    else {
    //将数据追加方式写出
    [self.filestream write:[data bytes] maxLength:[data length]];
    //修改进度条
    self.Acturelength += [data length];
    [_mydownCondition lock];
    controller.Acturelength=controller.Acturelength+[data length];
    controller.progress.progress= (controller.Acturelength*1.0)/controller.Myfilelength;
    [_mydownCondition unlock];
    }
    }
    }
      

  3.   


    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
        //得到要下载文件的文件大小、文件名称   
    long length=[response expectedContentLength];
    self.Myfilenmame=[response suggestedFilename]; 
    self.Myfilelength=length;
    //将文件名显示
    self.label.text=Myfilenmame;
        long firstleng = self.Myfilelength/3;
    for (int i=0; i<2; i++) {
    [self.r release];
    self.r = [[NSMutableURLRequest alloc] initWithURL: url];

    NSString *range = [[NSString alloc] initWithFormat:@"bytes=%d-%d", i*firstleng, (i+1)*firstleng];
    [self.r setValue:range forHTTPHeaderField:@"Range"];
    [range release];

    NSString *tempname = [[NSString alloc] initWithFormat:@"%@%d.tmp", self.Myfilenmame, i];
    TheardDown *threadload = [[TheardDown alloc] init];
    [threadload init_all:tempname controllers:self mylength:firstleng myrequest:self.r];
    [theards addObject:threadload];
    [threadload release];
    [tempname release];
    }

    self.r = [[NSMutableURLRequest alloc] initWithURL: url];

    NSString *range = [[NSString alloc] initWithFormat:@"bytes=%d-", 2*firstleng];
    [self.r setValue:range forHTTPHeaderField:@"Range"];
    [range release];

    TheardDown *threadload = [[TheardDown alloc] init];
    NSString *tempname = [[NSString alloc] initWithFormat:@"%@%d.tmp", self.Myfilenmame, 2];
    [threadload init_all:tempname controllers:self mylength:(self.Myfilelength - 2*firstleng) myrequest:self.r];
    [theards addObject:threadload];
    [threadload release];
    [tempname release];
    //设置几根线程在下载
    self.count = 3;
    [self.connection cancel];
    }
      

  4.   

    //可以通过设置代理来自动更改进度条的运动
    [downloadRequest setDownloadProgressDelegate: progressView];只要下载的进度改变 进度条(progressView)就可以自动改变
    我的问题是如何在现实?%的时候可以自动更新显示 而不是手动的一直重复调用函数 重复调用函数会影响其他线程的运行。不知道上述方法可不可以实现啊???学习中
      

  5.   

    iphone 父窗口与子窗口间的变量传递 各位有没有更好办法,请指教.
    请到我的博客  http://blog.csdn.net/fanxiaoduo
      

  6.   

    那个进度的显示, 可以根据关注UIProgressView 的 progress 值 进行改变, 也可以由时间来控制,在指定的时间内进行进度的更新。