I have a folder named "test" in my resources. I have a lot of images in there. How can I load all the images in the folder "test" in an array?
I tried with:
testArr = [[NSArray alloc] initWithArray:[[NSBundle mainBundle] pathsForResourcesOfType:#"gif" inDirectory:#"Test"]];
But it doesn't work!
Thx for your help!
greez franhu
pathsForResourcesOfType:inDirectory: gets you the filenames. Assuming you want the actual images, you might do:
NSArray *fileNames = [[NSBundle mainBundle] pathsForResourcesOfType:#"gif" inDirectory:#"Test"];
for(NSString *fileName in fileNames)
{
// load the file here and put it in another array
}
Other things to check:
Open up your application bundle and verify that the Test directory exists in it.
If you're doing this on iOS, consider using [UIImage imageNamed:] instead, which will search your bundle for the image in question automatically.
Related
I created the Settings Bundle
I done it. Because i need serf inside the bundle as the folder. For Example FileManager.
Below you can see.
And I opened meeting.Bundle in Finder using show contents and put into the Folders with pdf files. I try get these pdf files it on the device (iPhone 5) with code
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:#"meeting" ofType:#"bundle"];
NSString *filePathPdf = [[NSBundle bundleWithPath:bundlePath] pathForResource:theFileName ofType:#"pdf"];
But *filePathPdf is nil. Xcode cannot find them.
What i do wrong?
PS: I found the way how to load pdf files but i think this is wrong method.
I put into meeting.bundle the folder with pdf files and put the same folder into main bundle.
Load with path:
NSString *fileNamePdf = [self.contents objectAtIndex:indexPath.row];
NSString* theFileName = [[fileNamePdf lastPathComponent] stringByDeletingPathExtension];
NSString *filePathPdf = [[NSBundle mainBundle] pathForResource:theFileName ofType:#"pdf"];
Pdf Files loading from main bundle. But references of pdf files are taken from meeting.bundle
Maybe somebody knows how to make the right to work?
here is the code i am working with.
NSString* path = [[NSBundle mainBundle] pathForResource:#"file" ofType:#"plist"];
if(path == nil)
{
NSLog(#"file not found");
}
when i run this it prints file not found in the console.
my file.plist is in the supporting files folder in my cocoa project. where does mainBundle look for files at exactly. this is stumping me quite a bit. i get that it looks for the .app file, but when developing the app where doe mainBundle look?
pathForResource:ofType: only looks in the Resources folder (and localised folders). If you are placing files into dedicated folders in the bundle then you need to use pathForResource:ofType:inDirectory: and supply the directory name.
If the NSBundle you found is what you wanted through :
NSBundle *yourTargetBundle = [NSBundle mainBundle];
or
NSBundle *yourTargetBundle = [NSBundle bundleWithURL:[[NSBundle mainBundle]
URLForResource:#"YourCustomBundleName" withExtension:#"bundle"]];
And get the nil result use function: pathForResource: ofType:. Then you can log the paths of resource under yourTargetBundle:
// Given that your resource type is .png and have no sub dir
NSArray *resoursePaths = [yourTargetBundle pathsForResourcesOfType:#"png" inDirectory:nil];
NSLog(#"resoursePaths == %#",resoursePaths);
it will log all the resources path of type png. You can get the image object use:
targetImg = [[UIImage alloc] initWithContentsOfFile:#"OnePngPathYouChose"];
I think the above approach is one way to resolve the return nil situation.
I am quite new in iOS development and Objective-C and I would appreciate some help.
I created a Group folder in Xcode where I placed all my images. All my images are inside the project folder.
I want to iterate through the Xcode folder routineIcons and get all Images that have b- in their name and put their name and extension into an array.
Could anyone point me in the right direction on how to do this? bah.. is it possible??
You can use this snipped:
NSMutableArray *result = [NSMutableArray array];
[[[NSBundle mainBundle] pathsForResourcesOfType:#"png" inDirectory:nil] enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
NSString *path = [obj lastPathComponent];
if ([path hasPrefix:#"b-"]) {
[result addObject:path];
}
}];
At runtime, the routineIcons folder won't exist. All of those images, along with all other resources, will end up in the app's resource bundle.
What you can do is get the path to the resource bundle then use NSFileManager to get all of the files in the folder. You can then ignore any that don't start with "b-".
In xcode, I have create a folder called IMAGES and copied all of the images I will be using into that folder.
I am updating a table using the following code to insert the image:
NSString *picName = [NSString stringWithFormat:#"%#.jpg", [[theemplyees objectAtIndex:indexPath.row] valueForKey:#"EmpNo"]];
cell.empPicture.image = [UIImage imageNamed:picName];
Note the no location needing supplied. What I want to do is, if a users picture does not exist in the directly, then set the image to a default image. Such as:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//Set image
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:picName];
//Check if the image exists at a given path
BOOL imageExists = [[NSFileManager defaultManager] fileExistsAtPath:imagePath];
//If the image doesn't exists download it.
if (!imageExists)
{
picName = #"nopicture.jpg";
}
The problem is, using the above, it ALWAYS says the files don't exist. NONE! Grrr
So am i placing the files in the wrong location? I attempted to create a directory called Resources and place them there but still didn;t work.
Any help is greatly appreciated. Thanks in advance for any and all help.
Geo...
Nothing will be in the app's Documents directory unless you store something there during runtime. The Documents directory and the app's resource bundle are two completely different things.
If you created a folder in Xcode for your images, those images are still stored in the app's resource bundle, not the Documents folder.
The UIImage imageNamed: method only looks in the resource bundle.
I have been tearing my hair trying different combinations of file paths and different types but basically what I am trying to do is copy a folder called "test" that is inside of my resources folder.
Copying folders onto the NSPasteBoard works when I give an absolute path (ex: /Users/dw/src/Menulet) but it doesn't work when I try using my mainBundle's resource path.
Here is the code I currently have:
NSString *resourceFolder = [[NSURL fileURLWithPath:[[NSBundle mainBundle]
resourcePath]] absoluteString];
NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
[pboard declareTypes:[NSArray arrayWithObject:NSURLPboardType] owner:nil];
NSString *SDKPathString = [[NSString alloc] initWithFormat:#"%#test/",
resourceFolder];
NSURL *SDKPathURL = [[NSURL alloc] initWithString:SDKPathString];
[pboard writeObjects:[NSArray arrayWithObject:SDKPathURL]];
The result is that it can't find the file:
__CFPasteboardIssueSandboxExtensionForPath: error for [/Users/dw/Library/Developer/Xcode/DerivedData/Menulet-bvwpfkjlcufhxubvaxubgnubtfgi/Build/Products/Debug/Menulet.app/Contents/Resources/test]
How can I copy the directory?
Edit:
My guess is that you're missing a '/' character when you do this:
NSString *SDKPathString = [[NSString alloc] initWithFormat:#"%#test/",
resourceFolder];
Indeed resourceFolder is a path, I usually use something like this instead:
NSString* SDKPathString= [resourceFolder stringByAppendingPathComponent: #"test"];
Even if in your case you could simply correct the error by putting a '/' character before "test".But I think this is more mnemonic.
Update
Isn't said that if you create a group Xcode also creates a folder. If you want to be sure that Xcode does so, create a folder with finder and drag it to the project. Check these options:
This way the folder is actually created, and also added to the bundle.
Update 2
You shouldn't try to move your directory inside the project folder to put into the right place, instead put it wherever you want in the bundle, provided that the directory is copied in the bundle.
Once did so, the bundle will manage everything for you. So just search the directory using:
- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension;
So in your case:
NSBundle* bundle= [NSBundle mainBundle];
NSString *SDKPathString = [ bundle pathForResource: #"test" ofType: #""];