-1

For an app synchronized with a website, can anyone recommend a good way to check if an image exists locally?

I want to check if it is already downloaded to the phone before downloading it from the web.

I am currently storing the images locally in the documents folder as recommended by apple and retrieving it via a function that includes the following code.

- (UIImage*)loadImage
{
...
    UIImage* image = [UIImage imageWithContentsOfFile:path];
        return image;
}

Can anyone suggest a way to check of there is in a fact a local file?

user1904273
  • 4,562
  • 11
  • 45
  • 96

2 Answers2

1

This should be as simple as a call to NSFileManager's method - fileExistsAtPath:

NSString *filePath = //set your file path to the image's
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
    UIImage *image = [UIImage imageWithContentsOfFile:filePath];
    //the file exists
}
else
{
    //the file does not exist, fetch it from the server
}
Chris Loonam
  • 5,735
  • 6
  • 41
  • 63
1

if you set up your file system differently or looking for a different way of setting up a file system and then checking if a file exists in the documents folder heres an another example. also show dynamic checking

for (int i = 0; i < numberHere; ++i){
    NSFileManager* fileMgr = [NSFileManager defaultManager];
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString* imageName = [NSString stringWithFormat:@"image-%@.png", i];
    NSString* currentFile = [documentsDirectory stringByAppendingPathComponent:imageName];
    BOOL fileExists = [fileMgr fileExistsAtPath:currentFile];
    if (!fileExists){
        cout << "DOESNT Exist!" << endl;
    } else {
        cout << "DOES Exist!" << endl;
    }
}
Maulik shah
  • 1,664
  • 1
  • 19
  • 45