asihttprequest图片下载

时间:2012-02-06 19:46:40

标签: ios asihttprequest

大家好,大家好。我正在使用asihttprequest进行一系列图像下载 - 来自服务器的4张图像。但是我注意到了一些问题并且无法找到解决方案。假设我正在通过URL下载4张图片,如果任何一张或两张图片不可用,它将取消整个队列。

这是我的代码:

    [networkQueue setDownloadProgressDelegate:progressIndicator];
    [networkQueue setRequestDidFinishSelector:@selector(imageFetchComplete:)];
    [networkQueue setRequestDidFailSelector:@selector(imageFetchFailed:)];

    request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://imagees/image1.jpg"]];
    [request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"1.png"]];
    [request setDownloadProgressDelegate:imageProgressIndicator1];
    [request setUserInfo:[NSDictionary dictionaryWithObject:@"request1" forKey:@"name"]];
    [networkQueue addOperation:request];

    request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"sdvdsvsadvsadv"]] autorelease];
    [request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"2.png"]];
    [request setDownloadProgressDelegate:imageProgressIndicator2];
    [request setUserInfo:[NSDictionary dictionaryWithObject:@"request2" forKey:@"name"]];
    [networkQueue addOperation:request];

- (void)imageFetchComplete:(ASIHTTPRequest *)request
{
    UIImage *img = [UIImage imageWithContentsOfFile:[request downloadDestinationPath]];
    if (img) {
        if ([imageView1 image]) {
            if ([imageView2 image]) {
                [imageView3 setImage:img];
            } else {
                [imageView2 setImage:img];
            }
        } else {
            [imageView1 setImage:img];
        }
    }
}

- (void)imageFetchFailed:(ASIHTTPRequest *)request
{
    if (!failed) {
        if ([[request error] domain] != NetworkRequestErrorDomain || [[request error] code] != ASIRequestCancelledErrorType) {
            UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Download failed" message:@"Failed to download images" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
            [alertView show];
        }
        failed = YES;
    }
}

问题是假设第二张图像失败,它会显示错误信息并停止整个操作,尽管图像1是有效的图像文件。

非常感谢任何帮助。

:)

1 个答案:

答案 0 :(得分:5)

来自ASIHTTPRequest documentation

  

当ASINetworkQueue中的请求失败时,默认情况下该队列   取消所有其他请求。您可以使用[queue]禁用此行为   setShouldCancelAllRequestsOnFailure:NO]

相关问题