Search code examples
objective-ciosxcodensfilemanager

After Update, App Doesn't Load Existing Files from Documents Folder


I'm having an issue with loading existing images from the App's Documents Folder after ad-hoc update.

I've searched the internet for answers and I've found that I must use relative paths to files in order for the path to remain the same when the App is updated.

Can someone please show me how to do it ?

Right now I use the following to save the files (images) when ImagePicker finishes:

NSString *imageFilename = [NSString stringWithFormat:@"%@-profilePhoto.jpg",[NSDate date]];
NSArray *paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

imagePath = [NSString stringWithFormat:@"%@/%@", paths, imageFilename];

Please help me out !


Solution

  • I've found the solution:

    Instead of loading the images from the full path, I chose to make the system search for them after their name.

    So instead of:

    //Full path stored in a dictionary:
        profilePhotoPath = [userDict objectForKey:@"profilepic"];
    //Load the image from path:
        profilePhoto = [[UIImage alloc] initWithContentsOfFile:profilePhotoPath];
    

    I now use the following:

    //Load image from documentsDirectory, filename
        NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        profilePhotoPath = [userDict objectForKey:@"profilepic"];
        profilePhotoPath = [documentsDirectory stringByAppendingPathComponent:[profilePhotoPath lastPathComponent]];
        profilePhoto = [[UIImage alloc] initWithContentsOfFile:profilePhotoPath];
    

    By using "lastPathComponent" attribute I'm basically stripping out everything from the path except the filename and then I use NSSearch to give me my file.