将文件夹从主软件包复制到iphone中的文档目录 [英] Copy Folder from main bundle to documents directory in iphone

查看:167
本文介绍了将文件夹从主软件包复制到iphone中的文档目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,其中我有一个名为Images在我的主包中的文件夹。

I am having an app in which I have a folder named "Images" in my main bundle.

在此文件夹中有另一个文件夹Images1包含一些图片。

In This folder there is another folder called "Images1" containing some images.

当我的应用程序启动时,我想要文件目录中的文件夹Images。

When my app Launch I want the folder "Images" on the documents directory.

我要从文件夹Images1中获取图像。

I want to fetch images from the folder "Images1".

但我无法使用以下代码在文档目录的文件夹Images中获取我的图片。

But I can't get my images in the folder "Images" at documents directory with the following code.

已编辑

       BOOL success1;
    NSFileManager *fileManager1 = [NSFileManager defaultManager];
    NSError *error1;
    NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory1 = [paths1 objectAtIndex:0];
   NSString *writableDBPath1 = [documentsDirectory1 stringByAppendingPathComponent:@"Images/Images1"];
    success1 = [fileManager fileExistsAtPath:writableDBPath1];

    if (success1) return;
    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath1 = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Images/Images1"];

    NSLog(@"Default : %@",defaultDBPath1);
    success1 = [fileManager1 copyItemAtPath:defaultDBPath1 toPath:writableDBPath1 error:&error1];

    if (!success1) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error1 localizedDescription]);

    }

请帮助我。

推荐答案

您确定您的文件夹树中有这些文件吗?

Are you sure that you have the files in your folder tree?

NSString *defaultDBPath1 = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"/Images/Images1"];

所以首先检查你有你的源文件夹中的所有东西。即使您看到文件夹显示在软件包资源管理器中,在应用程序中复制时,您将看到图像位于根文件夹中。为了避免这种情况,当您添加Image文件夹时,在xcode项目中,您可以选中创建文件夹引用选项。

usually when xcode make a package it flatten the directory contents. So first check you have all the things in your source folder. Even if you see that the folder is shown in your package explorer, when copied in the app, you will see the images are in the root folder. To avoid this, when you add the Image folder, in your xcode project, you can check the option to 'create folder reference'.

但是,您的写入可能会失败,因为覆盖。在这种情况下,您必须为下面给出的 NSFileManager 实现一个委托方法。

However, your write may fail because of an overwrite. In that case you have to implement a delegate method for NSFileManager given below.

- (void) copyFolder {
    BOOL success1;
    NSFileManager *fileManager1 = [NSFileManager defaultManager];
    fileManager1.delegate = self;
    NSError *error1;
    NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory1 = [paths1 objectAtIndex:0];
    NSString *writableDBPath1 = [documentsDirectory1 stringByAppendingPathComponent:@"/Images"];
    success1 = [fileManager1 fileExistsAtPath:writableDBPath1];
    if (success1 )
    {

        NSString *defaultDBPath1 = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Images"];
        NSLog(@"default path %@",defaultDBPath1);
        success1 = [fileManager1 copyItemAtPath:defaultDBPath1 toPath:writableDBPath1 error:&error1];
    } else {
        if (![[NSFileManager defaultManager] fileExistsAtPath:writableDBPath1])
            [[NSFileManager defaultManager] createDirectoryAtPath:writableDBPath1 withIntermediateDirectories:NO attributes:nil error:&error1];
    }
}

- (BOOL)fileManager:(NSFileManager *)fileManager shouldProceedAfterError:(NSError *)error copyingItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath{
    if ([error code] == 516) //error code for: The operation couldn’t be completed. File exists
        return YES;
    else
        return NO;
}

这篇关于将文件夹从主软件包复制到iphone中的文档目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆