简体   繁体   中英

Copy Folder from main bundle to documents directory in iphone

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

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

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

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

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

Edited

       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]);

    }

Please help me.

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

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

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'.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM