【问题标题】:How to save and retrieve a downloaded file using ASIHTTPRequest如何使用 ASIHTTPRequest 保存和检索下载的文件
【发布时间】:2016-12-25 15:52:00
【问题描述】:

我正在使用 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];

            }
        }
    }
}

【问题讨论】:

    标签: iphone multithreading download asihttprequest


    【解决方案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
    {
    }
    

    如果有帮助请告诉我!!!

    【讨论】:

    • 我有一些疑问,如何在我的代码中调用 - (void)saveImage:(UIImage*)image:(NSString*)imageName?不能说代码名称,文件类型可以多变。
    【解决方案2】:

    首先,决定是使用块还是使用委托函数。

    如果你想使用块,那么由于 ASIHTTPRequest 是 NSOperation 的子类,你可以使用 -setComplectionBlock 来创建一个回调,当这个特定请求完成下载时将调用该回调

    如果您想使用委托函数,请将每个请求的委托设置为您想要的对象(可能是您的控制器),然后在同一个对象中实现 -requestFinished 和 -requestFailed 以便您可以处理响应。

    每次 url 完成下载时,都会以下载文件的请求作为参数调用回调。此请求具有 -responseData 对象,当请求完成时,它将填充下载的数据。这将是您的图像数据。

    这是正常且最广为人知的方式。现在,您似乎是直接执行此操作,但您已将每个请求设置为写入磁盘上的同一个文件!你确定你会通过这种方式从多张图片中得到你想要的数据吗?在我看来,下载时图像数据会重叠!

    这是我要做的:我会下载每张图片,并且在下载完每张图片后,我会使用回调函数来重命名结果文件。之后,在同一个回调中,我会将实际文件名存储在控制器中的 NSMutableDictionary 中。像 [myDict setValue:"path/to/image.png" forKey:"myImage1"] 这样我可以稍后访问它们。

    >

    【讨论】:

    • 感谢您的回复。我想让我的应用程序也支持 pre-iOs 4.0。所以我认为我应该使用 ASIHTTPRequest。在代码中,我只是给出了一个示例名称,文件应该是动态的。那么,你能给我看一个示例代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    • 2016-10-03
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多