19

我正在使用下面的代码将图像保存在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;
}

该名称通常是下载图像的 url。

文件名的长度是否有限制?你知道有时网址可能会超长...

谢谢你

4

1 回答 1

34

查看 syslimits.h:91 中的 PATH_MAX 常量

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

您可以通过以下方式自己测试:

NSLog(@"%i", PATH_MAX);

只想确认一下。

于 2011-07-05T11:46:32.417 回答