【问题标题】:Copy folder from Resource to Document in iphone sdk将文件夹从资源复制到iphone sdk中的文档
【发布时间】:2012-03-02 05:09:12
【问题描述】:

我在资源中有一个名为 SketchImages 的子文件夹,其中包含 300 张 PNG 图像,我需要做的是在我的应用程序启动时将此文件夹复制到 iphone sdk 的文件夹中,我正在尝试以下代码但没有运气

BOOL success;
NSFileManager *fileManager = [[NSFileManager defaultManager] init];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
                                                     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDBFolderPath = [documentsDirectory     
stringByAppendingPathComponent:@"SketchImages"];
success = [fileManager fileExistsAtPath:documentDBFolderPath];

if (success)
{
    return;
}else
{
    NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath]
                                      stringByAppendingPathComponent:@"DB"];
    //[fileManager createDirectoryAtPath: documentDBFolderPath attributes:nil];
    [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath           
                          error:&error];
}

任何帮助都会得到高度评价

【问题讨论】:

  • 为什么要将所有图像复制到文档目录?它会增加您在 iphone 中的应用程序大小...
  • 我正在写一本图画书,我需要这样做
  • 调用 copyItemAtPath 时返回什么错误?
  • NSArray *paths = [[NSArray alloc] initWithArray:[[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:nil]]; NSFileManager *fileManager = [[NSFileManager defaultManager] init]; NSArray *documentpaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *documentsDirectory = [documentpaths objectAtIndex:0]; NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"Images"]; NSError *错误; [[NSFileManager defaultManager] copyItemAtPath:[paths objectAtIndex:0] toPath:documentDBFolderPath error:err];

标签: iphone objective-c ios ios4


【解决方案1】:

您使用了错误的子文件夹名称:

NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath]
    stringByAppendingPathComponent:@"SketchImages"];

【讨论】:

  • @AbdulKarimKhan 您“在资源中有一个名为 SketchImages 的子文件夹”,并且您正在尝试复制名为“DB”的子文件夹。由于您没有名为“DB”的 Resources 子文件夹,因此您的代码不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-15
  • 2010-11-11
  • 2023-01-11
相关资源
最近更新 更多