如何使URL加载请求超时?

时间:2014-03-06 15:00:16

标签: ios iphone objective-c

我正在使用this tutorial在我的应用中异步加载图片。我修改了该代码,以便将图片保存到iPhone的本地文件中,并可以在离线时加载。我想这样做,所以这个加载请求在一定的间隔(可能是15-20秒)后超时,并加载保存的文件而不是下载新的文件。我找到了让Web视图超时的方法,但我不知道如何使用异步方法来实现这一点。如何针对此代码加载网址的方式发出超时请求?

由于

编辑:如果无法连接到网站,以及图片下载时间过长,我想让它超时。

  - (void)viewDidLoad
{
    [super viewDidLoad];
    NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc]
                                        initWithTarget:self
                                        selector:@selector(loadImage)
                                        object:nil];
    [queue addOperation:operation];

}


- (void)loadImage {
    NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.TestURL.com/test.jpg"]];
    UIImage* image = [[UIImage alloc] initWithData:imageData];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"test.jpg"]];
    [imageData writeToFile:localFilePath atomically:YES];
    [self performSelectorOnMainThread:@selector(displayImage:) withObject:image waitUntilDone:YES];


}

2 个答案:

答案 0 :(得分:3)

如果您使用NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.TestURL.jpg"]];您正在创建同步连接,那么您无法取消它。你需要等到最后。

您应该按照How can I deal with connection problem when I use NSData?

中的说明使用NSURLConnection实施异步下载

答案 1 :(得分:0)

现在,我是这方面的新手,我意识到这是一种方法,但实际上没有回答这个问题,但我如何处理这种情况是写一个方法,首先在本地检查图像,如果它不存在,从网上加载并在本地保存,所以下次它就在那里。这是一些代码。

- (UIImage *)checkForLocalImageThenSave:(NSString *)name fromStringURL:(NSString *)url {

    NSLog(@"********** Start loading image **********\n\n");
    UIImage *image = [[UIImage alloc] init];

    NSString *localDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *profilePicName = [NSString stringWithFormat:@"/%@", name];
    NSString *profilePicNameOnline = [NSString stringWithFormat:@"%@", url];

    NSString *directoryWithProfilePicName = [localDirectory stringByAppendingString:profilePicName];

    NSLog(@"Looking for file: %@", directoryWithProfilePicName);
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:directoryWithProfilePicName];

    if (fileExists) {

        NSLog(@"File exists. Load: %@\n\n", directoryWithProfilePicName);
        image = [[UIImage alloc] initWithContentsOfFile: directoryWithProfilePicName];

        NSLog(@"********** Loading image done **********\n\n");


    } else {

        NSLog(@"File does not exist. Save: %@", directoryWithProfilePicName);

        // TO SAVE A JPEG FILE

        NSData *imageWithURL = [[NSData alloc] initWithContentsOfURL:[[NSURL alloc] initWithString:profilePicNameOnline]];

        NSLog(@"File at? %@", profilePicNameOnline);


        image = [[UIImage alloc] initWithData:imageWithURL];
        NSString *jpegFilePath = directoryWithProfilePicName;
        NSData *data = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];//1.0f = 100% quality
        [data writeToFile:jpegFilePath atomically:YES];


        NSLog(@"Saving image done.");

    }

    return image;
}