【问题标题】:Read Image file from custom directory using NSBundle使用 NSBundle 从自定义目录读取图像文件
【发布时间】:2011-09-25 13:24:30
【问题描述】:

创建一个包含所有图像的自定义目录。自定义设计,因为它可以帮助我在需要时在配置中的各个位置获取图像。

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

我已将图像放在 test 文件夹下,并且是 .png 类型的。 有没有办法检索如下图像。

/** 再次更新**/

目前此文件夹位于 Application_Home/Resources/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;
}

【问题讨论】:

  • 你有什么解决办法吗?我也面临同样的问题

标签: iphone objective-c nsfilemanager nsbundle


【解决方案1】:

在 iOS 上运行的应用程序是沙盒的;你不能简单地在任何你喜欢的地方创建目录。您的 createDirectoryAtPath: 呼叫将失败。你应该改用one of the directories set aside for your application

一旦您获得其中一个目录的路径,获取其中文件的路径就是使用NSStringstringByAppendingPathComponent: 方法的一个例子。

【讨论】:

  • 嗨,吉姆。最初我真的认为 ios 将无法从我喜欢的目录中读取文件。我已经更新了我的问题。现在如何使用 NSString 的 stringByAppendingPathComponent 来获取图像?
  • 我希望根据它们的名称将图像插入到各种文件夹中,例如:/lifemoveson/test 或 /lifemoveson/test1 或 /lifemoveson/test2。这样以后我就知道要查看哪个文件夹,而不必担心路径名。
  • 是的,但你坚持哪一部分?您是否尝试过使用我提供的解决方案?
  • 我再次编辑了这个问题。我不确定如何通过简单地使用 NSString 的 stringByAppendingPathComponent 来获取文件:?
  • 那么你有什么?你有一个带有目录路径的NSString,你有PNG文件的名称,你有一个将它们添加在一起的方法。你有你需要的一切。您所要做的就是使用 PNG 文件的名称在目录路径上调用该方法,它会为您提供 PNG 文件的路径。
【解决方案2】:

调用诸如

之类的函数
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;
}

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

【讨论】:

  • 谢谢拉胡尔。但我想我还没有准确。我再次改变了我的问题。让我知道上述情况是否可能?
  • 我不知道你想在哪里创建目录?您可以在设备上运行的应用程序的文档文件夹中创建自定义目录。路径@"/Users/home/lifemoveson/test" 有点模棱两可。
  • 谢谢..我猜因为iphone设备就像一个沙箱,它只能在应用程序文件夹中创建文档文件夹。因此我的查询在 iphone 上是模棱两可的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-18
  • 2013-08-22
  • 2014-08-26
  • 2014-01-21
  • 2014-10-24
  • 2019-12-16
相关资源
最近更新 更多