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 回答