1

ASIHTTPRequest を使用して複数のダウンロードを行っています。ここでは非同期ダウンロード方式を使用しているため、複数のダウンロードを実行できます。正常に動作しています。今、これらのダウンロード ファイルをアプリケーションのローカル フォルダー (デバイス内) に保存し、それを取得したい (各ファイルへのパスを取得したい)。これどうやってするの?ここでは複数のダウンロードが実行されているため、ダウンロード内の各ファイルをどのように区別できますか? これが私が使用したコードです。

- (void)download{
    UIImageView *image = (UIImageView *)[mainView viewWithTag:selectedTag];

    for (UIProgressView *currentProgress in [image subviews]) {
        if ([currentProgress isKindOfClass:[UIProgressView class]]) {
            NSLog(@"Prog tag: %d",currentProgress.tag);
            if(currentProgress)
            {
                currentProgress.progress = 0.0;
                ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[urlArray objectAtIndex:selectedTag]]];
                [request setDownloadProgressDelegate:currentProgress];
                [request setShowAccurateProgress:YES];                
                [request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"mansory_porsche_carrera_3-1920x1200.jpg"]];
                [request shouldContinueWhenAppEntersBackground];
                [request allowResumeForFileDownloads];
                [request startAsynchronous];

            }
        }
    }
}
4

2 に答える 2

1

それに応じてこれらの関数を変更し、必要なファイルを保存できます。

- (NSString *)applicationDocumentsDirectory 
{
  return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

- (void)saveImage:(UIImage*)image:(NSString*)imageName 
{
 NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.

 NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it

 NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory

 NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path 

 [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)

 NSLog(@"image saved");
}

取得するために、これらの行を使用してファイルが存在するかどうかを確認できます。

NSString *cachedImageFileUrl = [documentsDirectory stringByAppendingPathComponent:yourFileName];

NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath])
{
  //File exists...
}
else
{
}

それが役立つかどうか教えてください!!!

于 2012-05-03T06:51:45.663 に答える
1

まず、ブロックを使用するか、デリゲート関数を使用するかを決定します。

ブロックを使用する場合、ASIHTTPRequest は NSOperation のサブクラスであるため、-setComplectionBlock を使用して、この特定のリクエストのダウンロードが完了したときに呼び出されるコールバックを作成できます。

デリゲート関数を使用する場合は、各リクエストのデリゲートを目的のオブジェクト (場合によってはコントローラー) に設定し、同じオブジェクトに -requestFinished と -requestFailed を実装して、応答を処理できるようにします。

URL のダウンロードが完了するたびに、ファイルをダウンロードしたリクエストを引数としてコールバックが呼び出されます。このリクエストには -responseData オブジェクトがあり、リクエストが完了すると、ダウンロードされたデータが取り込まれます。これが画像データになります。

これは通常の、最もよく知られている方法です。今、あなたはそれを直接やっているように見えますが、ディスク上の同じファイルに書き込むように各リクエストを設定しました! この方法で複数の画像から必要なデータを取得できますか? ダウンロード中に画像データが重なってしまうようです!

これが私がすることです: 各画像をダウンロードし、各画像をダウンロードしている間に、コールバック関数を使用して結果のファイルの名前を変更します。その後、同じコールバック内から、コントローラの NSMutableDictionary 内に実際のファイル名を保存します。[myDict setValue:"path/to/image.png" forKey:"myImage1"] のようなもので、後でアクセスできるようにします。

<< 余談ですが、AIHTTPRequest は開発者が放棄したので放棄したほうがいいと思います。AFNetworking を優れた代替手段として使用できます。また、進行状況のコールバックも提供し、ブロックベースです。iOS 4.0 より前の互換性が必要な場合は、最後の段落を無視してください。>>

于 2012-05-03T07:04:34.330 に答える