Reputation: 1421
so, I am creating one Directory in Document Directory and copy list of file from Bundle path Directory named "ImageFolder" as shown in Screenshot.
I tried different code but always I get the Array nil.I know that there is no folder at the path which i get but how to do please tell me.
-What I want >
Create Directory "xyz" in Documents Directory.
copy all files from "ImageFolder" into "xyz".
//================================================================
//================================================================
Upvotes: 1
Views: 261
Reputation: 1245
What you have currently is a group (yellow folder icon) (physically they are not in a subfolder) - What you need to do is to create a folder outside Xcode, put in the images that you need then drag it into your project - You should have a blue folder icon and your command should work.
One other thing, just reference it, that way you do not have to keep on introducing your images to Xcode every time it will just pick it up she you compile and put it into the bundle.
Upvotes: 0
Reputation: 2369
This code works
-(void) copyDirectory:(NSString *)directory {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:directory];
NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:directory];
if (![fileManager fileExistsAtPath:documentDBFolderPath]) {
//Create Directory!
[fileManager createDirectoryAtPath:documentDBFolderPath withIntermediateDirectories:NO attributes:nil error:&error];
NSArray *fileList = [fileManager contentsOfDirectoryAtPath:resourceDBFolderPath error:&error];
for (NSString *s in fileList) {
NSString *newFilePath = [documentDBFolderPath stringByAppendingPathComponent:s];
NSString *oldFilePath = [resourceDBFolderPath stringByAppendingPathComponent:s];
if (![fileManager fileExistsAtPath:newFilePath]) {
//File does not exist, copy it
[fileManager copyItemAtPath:oldFilePath toPath:newFilePath error:&error];
} else {
[fileManager removeItemAtPath:newFilePath error:&error];
[fileManager copyItemAtPath:oldFilePath toPath:newFilePath error:&error];
}
}
[self info:@"Copy finish!"];
}
}
In directory add the name of your folder to copy
Edit:
Your folder must be added by reference. Drag your folder into your project in xCode and select "Create folder references for any added folders"
Upvotes: 1
Reputation: 119041
The screenshot you show of ImageFolder
is a group in the Xcode project, that doesn't mean that anything related to that group will be copied into the app. If the images in that app are in your copy bundle resources build phase then they will be copied directly into the app bundle (not a sub folder).
You need to add a new copy files build phase, set the folder name, and move the images into that folder.
Once you have done that, consider why you want to copy the images. Can the user do some editing? If not then copying the images is a waste of space. If the user can 'delete' some of the images then think about storing a 'deleted items' plist or user default to just hide / filter the list of images.
Upvotes: 1