2011-06-29 55 views
0

创建包含所有图像的自定义目录。设计它是自定义的,因为它会帮助我在配置中的各个位置获取图像。使用NSBundle从自定义目录中读取图像文件

NSFileManager *filemgr; 
filemgr = [NSFileManager defaultManager];  
[filemgr createDirectoryAtPath: @"/Users/home/lifemoveson/test" withIntermediateDirectories:YES attributes: nil error:NULL]; 

我放置了测试文件夹下的图片,并且是.png类型。 有没有办法像下面那样检索图像。

/**再次更新**/

目前这个文件夹正在APPLICATION_HOME /资源/ ImageTiles /按Photoscroller例子。 我们如何将它更改为/ Users/home/lifemoveson/test/ImageTiles /文件夹?

- (UIImage *)tileForScale:(CGFloat)scale row:(int)row col:(int)col 
{ 
// we use "imageWithContentsOfFile:" instead of "imageNamed:" here because we don't want UIImage to cache our tiles 
NSString *tileName = [NSString stringWithFormat:@"%@_%d_%d_%d", imageName, (int)(scale * 1000), col, row]; 

// Currently this folder is under <Application_Home>/Resources/ImageTiles/ as per Photoscroller example. 
// How can we change it to /Users/home/lifemoveson/test/ImageTiles/ folder ? 
NSString *path = [[NSBundle mainBundle] pathForResource:tileName ofType:@"png"]; 
UIImage *image = [UIImage imageWithContentsOfFile:path]; 
return image; 
} 
+0

你有没有得到任何解决方案?我也面临同样的问题 – priya

回答

0

在iOS上运行的应用程序是沙箱;无论您想要什么,都无法简单地创建目录。您的createDirectoryAtPath:通话将失败。您应该使用one of the directories set aside for your application

一旦获得这些目录之一的路径,获取其中的文件的路径仅仅是使用NSStringstringByAppendingPathComponent:方法的情况。

+0

嗨吉姆。我真的认为,最初ios将无法从我喜欢的目录中读取文件。我更新了我的问题。现在我该如何使用NSString的stringByAppendingPathComponent来获取图像? – lifemoveson

+0

你坚持什么部分? – Jim

+0

我正在寻找按照名称插入图像的各种文件夹,如:/lifemoveson/test或/lifemoveson/test1或/lifemoveson/test2。以便将来我知道要查看哪个文件夹而不必担心路径名称。 – lifemoveson

0

马金如

NSString* newDirPath = [self createDirectoryWithName:@"Test"]; 
if (newDirPath) { 
    [self saveFile:@"MasterDB.sqlite" atPath:newDirPath]; 
} 

的功能调用,它是这样实现的

-(NSString*)createDirectoryWithName:(NSString*)dirName{ 

    NSArray* directoryArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask , YES); 
    NSString* directoryPath = [directoryArray objectAtIndex:0]; 
    NSString* newPath = [directoryPath stringByAppendingString:[NSString stringWithFormat:@"/%@",dirName]]; 
    NSFileManager *filemamager = [NSFileManager defaultManager];  
    BOOL flag = [filemamager createDirectoryAtPath:newPath withIntermediateDirectories:YES attributes: nil error:NULL]; 
    return flag == YES ?newPath: nil; 
} 

-(BOOL)saveFile:(NSString*)fileName atPath:(NSString*)path{ 

    BOOL success; 

    // Create a FileManager object, we will use this to check the status 
    // of the File and to copy it over if required 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    // Check if the File has already been created in the users filesystem 
    NSString *filePath = [path stringByAppendingPathComponent:fileName]; 

    success = [fileManager fileExistsAtPath:filePath]; 

    // If the File already exists then return without doing anything 
    if(success) return YES; 

    // If not then proceed to copy the File from the application to the users filesystem 

    // Get the path to the database in the application package 
    NSString *pathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName]; 

    // Copy the database from the package to the users filesystem 

    NSError *error = nil; 

    BOOL flag = [fileManager copyItemAtPath:pathFromApp toPath:filePath error:&error]; 

    return flag; 
} 

可能会帮助您解决问题。创建一个目录是使用第一个函数实现的,第二个可以让你将应用程序包中的文件保存到任何以前创建的目录中。我希望你可以修改它保存位于应用程序包以外的文件以适合你的需要。

+0

谢谢拉胡尔。但我想我还没有确切。我已经改变了我的问题。让我知道如果上述情况是可能的? – lifemoveson

+0

我无法弄清楚你想在哪里创建目录?您可以在设备上运行的应用程序的文档文件夹中创建自定义目录。 path @“/ Users/home/lifemoveson/test”有点含糊。 –

+0

谢谢..我猜测,因为iPhone设备就像一个沙箱,它只能在应用程序文件夹内创建文档文件夹。因此我的查询是不明确的iPhone。 – lifemoveson