max length of file name

astazed picture astazed · Jul 5, 2011 · Viewed 8.4k times · Source

i am using the code below to save an image in the NSDocumentDirectory

-(BOOL)saveImage:(UIImage *)image name:(NSString *)name{

    NSString *dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *path = [NSString pathWithComponents:[NSArray arrayWithObjects:dir, name, nil]];

    BOOL ok = [[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];

    if (!ok) {
        NSLog(@"Error creating file %@", path);
    } 
    else {
        NSFileHandle* myFileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
        [myFileHandle writeData:UIImagePNGRepresentation(image)];
        [myFileHandle closeFile];
    }
    return ok;
}

the name is usually the url of where the image was downloaded.

is there a constraint on the length of the file name? you know sometimes urls may be super long...

thank you

Answer

deanWombourne picture deanWombourne · Jul 5, 2011

Taking a look at the PATH_MAX constant in syslimits.h:91

... 
#define PATH_MAX         1024   /* max bytes in pathname */
...

You can test this yourself by doing :

NSLog(@"%i", PATH_MAX);

just to make sure.