1

Im using this tutorial to load an image asynchronously in my app. I modified that code so the picture saves to the iPhone's local files and can be loaded while offline. I want to make it so this load request times out after a certain interval, possibly 15-20 seconds, and loads the saved file instead of downloading a new one. I found ways to make a web view time out, but Im not sure how to go about doing this using the asynchronous method. How can I make a timeout request for the way that this code loads the url?

Thanks

Edit: I want to make it time out if it is unable to connect to the website and also if the downloading of the picture takes too long.

  - (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];


}
a-manza
  • 13
  • 5
  • Look here: possible repeat: http://stackoverflow.com/questions/2149929/how-to-know-when-nsdatas-initwithcontentsofurl-has-finished-loading – trumpetlicks Mar 06 '14 at 15:07

2 Answers2

3

if you use NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.TestURL.jpg"]]; You are creating a synchronous connection, so You can´t cancel it. You need to wait till the end.

You should implement an asynchronous download using NSURLConnection as explained in How can I deal with connection problem when I use NSData?

Community
  • 1
  • 1
Fran Martin
  • 2,369
  • 22
  • 19
  • The link you provided shows to how to make the connection request time out, correct? How could I make the downloading of the linked picture time out once it has already connected to the URL? – a-manza Mar 07 '14 at 15:02
  • You should set your timeout in your request, before to connect with the URL. `initWithURL:cachePolicy:timeoutInterval:` – Fran Martin Mar 09 '14 at 09:30
0

Now, I am a newbie at this to, and I realize that this is a way to do it but really doesn't answer the question, but how I handled this situation was to write a method that first checked locally for the image, and if it wasn't there, load it from the web and save it locally, so it was there the next time. Here is some code.

- (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;
}
StephenCollins
  • 785
  • 4
  • 10