I have Images folder inside the Resources folder which contains images. I am using the following code to access the images but the resultant array always comes out to be empty:
NSArray *imagesPath = [bundle pathsForResourcesOfType:#"gif" inDirectory:#"Images"];
Am I missing something??
If you do [[NSBundle mainBundle] pathForResource:#"Images" ofType:nil] do you get the correct path for that folder?
If not are you sure that is a folder and not a simple group?
Related
I have done the following steps.
1. Created an xcode project and created a folder named "Images" inside Supporting Files.
Dragged and dropped 4 images into it. Tried to access those files using the following code
NSString *pathString = [[NSBundle mainBundle] pathForResource:#"Images" ofType:nil];
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathString error: nil];
NSLog(#"the fileList is %d",[fileList count]);
The count is always 0. What should I do now? There are files inside it and I am able to use it in imageviews.
So what is the mistake that I am making?
The Xcode does not generate any folders in your app bundle that corresponds to groups. Any resources added are present in Copy Bundle Resources in your target's Build Phases. You then access your resource under [[NSBundle mainBundle] bundlePath] path.
But if you want to count files under any folder then while adding that folder you must check the option :
"Create folder references for any added folders."
This will create folders and sub-folders in the same hierarchy as you add them.Then you can easily count them in the same manner you are doing above...
Otherwise the app bundle has all of your resources at one place not in any folder as you say "Images".Use following code :
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[NSBundle mainBundle] resourcePath] error: nil];
NSLog(#"the fileList is %d",[fileList count]);
It will list all your resources.
Try this,
NSString *imagespath = [[NSBundle mainBundle] pathForResource:"yourimagename" ofType:#"png" inDirectory:#"images"];
In my application I obtain a title of PDF which is saved in my plist with static name which is i given like s.pdf
After that I find the title of that pdf and now I want to save the same pdf with that title name which I got and also replacing the old name which is s.pdf
So how can I do that?
You probably going to need NSFileManager copyItemAtPath:toPath:error:
Copies the directory or file specified in a given path to a different location in the file system identified by another path.
Example This is not a complete code it's for explanation only:
// This will get you the Bundle Path.
NSString*path = [[NSBundle mainBundle] resourcePath];
// This will get your PDF in the Bundle Path.
[[NSBundle mainBundle] pathForResource:#"s" ofType:#"pdf"]
// Copy your file to the same destination but with different name.
[[NSFileManager defaultManager] copyItemAtPath:OldFileNameInBundle
toPath:NewFileNameInBundle
error:&err]
NSFileManager copyItemAtPath
I'm having trouble figuring out why files in my iPhone app seem to persist, even when I've deleted them. Here's the code that's giving me trouble:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *folderPath = [bundlePath stringByAppendingPathComponent:#"filefolder"];
NSArray *fileNames = [fileManager contentsOfDirectoryAtPath:folderPath error:NULL];
This code is supposed to look at the folder "filefolder" and read its contents into fileNames. When I run this app the very first time, it will do this. But if I change the contents of filefolder (for instance if I add or delete files) and I build and run the app again, the array filenames will contain names of all the newly added files (good) but also contains names of all the files that were supposed to have been deleted (bad)!!
Can anyone help me understand why I'm seeing this behavior?
Did you do a "Clean" in XCode after adding/removing new items from the bundle? This usually solves the problem of "stale" resources.
I am using this code to get the list of Images in my project
NSArray *pngPaths = [[NSBundle mainBundle] pathsForResourcesOfType:#"png" inDirectory:nil];
I dont want to get one image from the list can i add filter to it
Thank You
I think it is easier to do when you already load all paths into your memory NSArray. You can just loop over all elements and examine the path and remove paths you don't want
This question tells how one can create directories in your resources path. Once created, how does one reference these directories?
I have created an html directory with a structure for my internal pages, now I want to load the index.html file from the html directory. Thus, I'll need the file path to it.
I can just use:
NSString *filename = [[NSBundle mainBundle] pathForResource:#"index"
ofType:#"html"];
But what happens if there are two index.html files contained in the directory structure? Does it just find the first one? Can the referencing be more specific?
Little test project here that is not working if you want to take a look
If you have a path that is copied into your resource bundle, you also should reference the path when looking for the resource like:
NSString *filename = [[NSBundle mainBundle] pathForResource:#"html/index" ofType:#"html"];
EDIT:
You cannot apparently just include the directory in the resource name (I believe you can for other similar calls like "imageNamed:" on UIImage). The working call is:
NSString *helpFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"html"];