NSbundle pathforresource not finding file - ios

I have images.xcassets listed ounder copy bundle resources, and I did try to just state the file name by itself: MSB_big_icon , before trying to add the path within images.xcassets.
Can anybody tell me what I'm doing wrong?
NSString *path = [[NSBundle mainBundle]pathForResource:#"/Raymio_android_images/MSB_big_icon.imageset/MSB_big_icon" ofType:#"png"];
NSLog(#"path: %#", path);
MSBIcon *tilecon = [MSBIcon iconWithUIImage:[UIImage imageWithContentsOfFile:path] error:&error];

David Ansermot is right that xcassets is a much better approach and strongly preferred. If you can't use that (running on older versions of iOS for instance), still put everything in one directory and use imageNamed:. This has significant caching benefits over hand-loading the file.
An asset catalog (xcassets) is a (relatively) new, unified way of managing image resources. The images are no longer accessible as separate files on the disk. Instead, imageNamed: consults the asset catalog and fetches the correct asset.
Prior to asset catalogs (and still, for non-images), assets were stored in localized directories. All of your unlocalized assets would be put into a directory called Resources (no matter where those files might appear to be in your source tree, and no matter how those files might be arranged in your Xcode folders). Localized files would be stored in directories like English.lproj or French.lproj. When you make NSBundle calls to load MyImage, it looks at each localized directory in the order the user has configured, and if it cannot find it in any of those directories, it looks in Resources.
Now it is possible to store full directories as "a resource" by marking them as directory references in Xcode. In that case, the whole directory would be copied into Resources or the appropriate localized directory. In order to find files inside such a directory you can use the ...inDirectory: version of the NSBundle methods.
So most of the time, you want to just use imageNamed:, which is going to fetch things out of the asset catalog if available, and then search localized directories, and then look in Resources. If you need to find a non-image, or if for some reason you want the real path to the file, you can compute it like this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"MSB_big_icon" ofType:#"png"];
And if that resource were in a directory tree (because it was a directory reference in Xcode), you can access it like this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"MSB_big_icon"
ofType:#"png"
inDirectory:#"Raymio_android_images/MSB_big_icon.imageset"];

Here's a code exemple from one of my apps :
NSString *appKey = #"Applications__GENERIC";
NSString *path = [[NSBundle mainBundle] pathForResource:appKey ofType:#"plist"];
appData = [NSDictionary dictionaryWithContentsOfFile:path];
The "Applications__GENERIC.plist" is stored like this :
Other solutions :
Use the images.xcassets.
Then in your code to load an image, use the code :
UIImage *image = [UIImage imageNamed:#"MyImageWithoutExtension"];
Don't put any path or extension, only the image's name

Try using this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"MSB_big_icon" ofType:#"png" inDirectory:#"Raymio_android_images/MSB_big_icon.imageset"];
What you can also do to debug is to print out
[[NSBundle mainBundle] bundleURL]
Then navigate to that folder and see if the folder structure corresponds to the path you use.
I just struggled with this today, hope it works out for you too.

Related

How to get a shorter path to a file of my Xcode project

I have to share my OS X app, all the paths I have used for files used by project are linked to my username and the structure of my computer.
Is there a way to have paths related to my project so that once my project is shared the user may not get in troubles caused by 'file not found'.
I would move the used files of the project, into the project but then I don't know how to let this happen:
actual paths, what I use now:
/Users/???username???/XCode/projectName/fileName.txt
what I would like to use in my code:
function(path: fileName.txt)
how don't know how to make the paths this short, not caring about the users directories since the files I'm going to use are all inside my project.
I am very confused. Thank you.
There is actually an easy way to read files from your project directory:
NSError *error = nil;
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"txt"];
NSString *dataContent = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
So in dataContent you have the content of the file as an NSString instance.

pathForDirectory method for NSBundle?

If I include a folder in my bundle (a real folder, the blue ones, not the yellow groups), how can I get the path for that folder from my bundle? The method I would usually use is...
[[NSBundle mainBundle] pathForResource:fileName ofType:______];
...but what "type" is a directory? Is there a "type" to use, or is there another method for accessing the paths of directories within the bundle?
Or am I going about this all wrong, and there's some other way for including folders of accessible documents in the bundle?
Directories can have extensions too. If yours doesn't have one, just pass #"" for the type parameter, -[NSBundle pathForResource:ofType:] works for directories too, not only files. At the end, a directory is also a resource :)
As per a suggestion in the comments, it turns out you can use an empty string to refer to folders. So a folder called "myFolder" would be accessible using...
[[NSBundle mainBundle] pathForResource:#"myFolder" ofType:#""];
Hope this helps anyone else who wasn't expecting it to be so easy...
As you're adding a folder by your own, you know the name of the folder, so you can get the path like this:
NSString *myDirectoryName = #"myDirectory";
NSString *absolutePathToMyDirectory = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:myDirectoryName];
You'll have to test if this path exist with NSFileManager.
Otherwise you can still use the method you're using, the type can be nil, so you can call it like this
NSString *absolutePathToMyDirectory = [[NSBundle mainBundle] pathForResource:myDirectoryName ofType:nil];
and then testing if the result is nil or not.
if(absolutePathToMyDirectory) {
// do stuff related to this path
}

Load Pods-acknowledgements.markdown as NSString

I'm building an application and want to load the cocoapods auto-generated acknowledgements markdown file into an NSString to be displayed in my application. I though it would be as simple, as doing this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Pods-acknowledgements" ofType:#"markdown"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
But this doesn't work.... Is there a way to access this file?
It doesn't look like this file is automatically copied into your project bundle.
You need to add this file to your Copy bundle Resource build phase. It's also worth nothing that the file name actually contains your project's name Pods-<PROJECT_NAME>-acknowledgements
In practice you may want to make a symbolic link in your project that points to the generated file in the Pods directory to ensure that the newly generated file is used each time

Image retrieve from folder with folder name in iOS

I have all my images in folder name ImageMe and I like to retrieve image with this name like
[UIImage imageNamed:#"ImageMe/bulk-female.jpg"];
how I get this approach, because I rereiving path name from data base, and in that format it's coming.
If you do not want to modify these images, just create folder name "Resource" under supporting files and save ImageMe folder with your images there. Then use below code to get path of image folder.
NSString *theResourcePath = [NSBundle mainBundle] resourcePath];
NSString *theFilePath = [theResourcePath stringByAppendingPathComponent:#"ImageMe/bulk-female.jpg"];
If you have the images in the main bundle you can get them with
[UIImage imageNamed:#"theNameOfTheImage_WithOutDirecotry"];.
You have to specify only the image name not the directory. This is because when the application is packed in .ipa only the images are copied (the images that are in the Copy Bundle Resources) not the directories.
If you want to pack the images in to one folder you have to create a new bundle but you can not use imageNamed you have to use initWithContentsOfFile:(NSString *)path
Here are some docs UIImage, NSBundle, Bundle Programming Guide
If your all images are present in bundle then you can directly get that images from name.
like this
[UIImage imageNamed:#"1.jpg"];
[UIImage imageNamed:#"2.jpg"]
[UIImage imageNamed:#"3.jpg"]
directly get the images using their names.
And if you want to laod that images to particular folder of bundle.
NSString *Path = [NSBundle mainBundle] resourcePath];
NSString *FilePath = [Path stringByAppendingPathComponent:#"ImageMe/bulk-female.jpg"];
try this one this might be helpful to you.
Add your images in a floder & than add that folder to your project like this
after adding the folder to your project, you can access your images with the following code.
NSString *bundlePath = [NSBundle mainBundle] bundlePath];
NSString *imagePath = [bundlePath stringByAppendingPathComponent:#"ImageFolder/imagename.jpg"];
Hope this will solve your problem

How to hardcode a text file into iOS app

Currently using:
NSString* path = [[NSBundle mainBundle] pathForResource:#"filename"
ofType:#"txt"];
This only works however after I manually add the file into the application bundle.
I can use the documents directory but that's even worse.
After resetting the sim, the file goes away. How do I get it to stay?
How would I write out the file from somewhere to there? Like from a file in source? I don't want to alloc a several megabyte NSString object.
Credit goes to Lyle42 on freenode irc (#iphonedev):
I wasn't aware this field even existed. By adding any file into the copy files build phase (under build phases), they persist across builds.
Then this code:
NSString *_filePath = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"txt"];
NSLog(#"%#",_filePath);
NSData *_binary = [NSData dataWithContentsOfFile:_filePath];
NSString *_fileContents = [[NSString alloc] initWithData:_binary encoding:NSUTF8StringEncoding];
Works just great. (loads contents of file into an NSString).
You can use this to get your path for the file
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [documentPaths objectAtIndex:0];
filePath = [path stringByAppendingPathComponent:file_name];
you can get the file from your app bundle using
NSString *pathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:file_name];
I can use the documents directory but that's even worse.
Not really. The documents directory is persistent, and the recommended directory for essential documents created by your application. In addition, it's backed up by iTunes, and can be specified for iCloud backup. The app bundle just can't compete with that!
After resetting the sim, the file goes away. How do I get it to stay?
You can't, once the simulator has been "reset" (and I mean a hard wipe of all data and apps), nothing is saved. In between test builds, however, data copied out of the app bundle should survive unless you're storing things in /tmp. In addition, objects in the bundle are "refreshed" (recopied) in the build phase, meaning your resource isn't wiped out, merely reset to the one specified in your project.
How would I write out the file from somewhere to there? Like from a
file in source? I don't want to alloc a several megabyte NSString
object.
To read the file into memory, it would have to turn into a "several megabyte NSString" eventually, but if you really want to add a little IO to your plans for a massive string object, have a look at NSFileManager.

Resources