1

私のアプリはメッセージ アプリで、画像ファイルを送信することもできます。Webサーバーに画像としてアップロードし、反対側ではそのURLを送信するだけです。NSURLConnectionを使用して、ダウンロードインジケーターとしてUIProgressViewを使用して画像をダウンロードしようとしています。ここに私のコードがあります:

このメソッドは、uitableview のダウンロード ボタンがクリックされたときに呼び出され、ダウンロード ボタンを削除し、uiprogressview を追加してダウンロードを開始します。

-(void)downloadImage:(UIButton *)link
{
    UITableViewCell *cell = (UITableViewCell*)[link superview];

    NSIndexPath *pathToCell = [tView indexPathForCell:cell];

    NSMutableDictionary *checkItHasFile = [messages objectAtIndex:pathToCell.row];

    NSString *str = [checkItHasFile objectForKey:@"hasFile"];

    if([str isEqualToString:@"1"])
    {
        progress = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar];
        progress.frame = CGRectMake(10, 50, 160, 30);
        progress.progress = 0.0;

        //progress.center = CGPointMake(23,21);

        [cell addSubview:progress];
    }

    UIButton *view = [[UIButton alloc]init];
    NSArray *subviews = [cell subviews];

    for (view in subviews)
    {
        if([view isKindOfClass:[UIButton class]])
        {
            [view removeFromSuperview];
        }
    }

    //

    NSString *linkToPass = [NSString stringWithFormat:@"THE URL"];

    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:linkToPass]
                                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:60.0];

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConnection)
    {
        nsmd = [[NSMutableData alloc]init];
    }
    else
    {
        NSLog(@"Connection to server failed!");
    }
...

このメソッドは、応答について示す NSURLConnection デリゲートです。これで、応答サイズを計算しています

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
        [self.resourceData setLength:0];
        self.filesize = [NSNumber numberWithLongLong:[response expectedContentLength]];
} 

このメソッドは、受信したデータについて示す NSURLConnection デリゲートです。計算を行ってプログレス バーを更新しています。

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    resourceData = [[NSMutableData alloc]init];
    [self.resourceData appendData:data]; 
    NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[self.resourceData length]];

    [self.progress setProgress:[resourceLength floatValue] / [self.filesize floatValue] animated:YES];
}

ダウンロードがいつ終了したかを知りたいので、デリゲート メソッドが NSURLConnection の connectionDidFinishLoading:connection であることの進行状況ビューを削除することもできます。

問題はすぐにトリガーされるため、進行状況ビューがダウンロードの進行状況をアニメーション化しているときにこのメソッドも実行されます。ここで進行状況ビューを削除すると、完全なダウンロードの進行状況を示すことなく進行状況ビューがすぐに消えます。

この問題を解決するには?

4

1 に答える 1

0

プログレスバーを削除するメソッドを定義し、- (void)connectionDidFinishLoading:(NSURLConnection *)connection実装で、リソースの長さが決定した特定の量よりも短い場合は、遅延して remove メソッドを呼び出します。

[self performSelector:@selector(removeProgressBar) withObject:nil afterDelay:2];

それ以外の場合は、遅滞なく同じメソッドを呼び出します。

于 2012-11-15T12:22:54.880 に答える