In my App for the iPad I'm trying to load an image file programmatically. The file is NOT part of my project, hence it is not referenced in XCode. There is no entry for the file in XCode's Groups and Files column.
The image has to be loaded at runtime instead, it name being read from a Property List.
I'm trying to load the file like this:
NSString* pathToImageFile = [[NSBundle mainBundle] pathForResource:#"MyImage" ofType:#"png" inDirectory:#"MyDirectory"];
UIImage* retVal = [UIImage imageWithContentsOfFile:pathToImageFile];
In this case, mydirectory lives in the main bundle like this:
MyAmazingApp.app/MyDirectory/MyImage.png
Unfortunately, the Image will not load. I can't add the image file to my project, as its name is to be determined at runtime and cannot be known in advance. The file name is read from a config file (a Property List) at runtime instead.
What am I doing wrong? Your help will be very much appreciated.
You can use NSFileManager to get the contents of a directory, or several directories. You obviously have to know something about the file you want to load so that you can identify it, but you could for example use NSFileManager to help you generate a list of images in your app bundle and in your app's Documents directory.
Related
When I create a new project with CoreDataModel created by XCode, all is well. However, I added a core model file to my project and the "core data stack" code. The code is properly getting invoked, however, the model's URL is nil. I have tried to examine the project and cannot determine if the model file is not properly added, or compiled, or if the URL just cannot be found.
Help me debug and resolve.
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:#"Model" withExtension:#"momd"];
modelUrl returns as nil.
Check for correct names, then click on the Model.momd file in the file inspector on the left. In the file attributes inspector on the right make sure it is added to the correct target you are running (i.e. the checkmark beside your target name is checked).
If you cannot find the file in the first place, you have not added it to the bundle correctly. Drag it from the Finder window into your project.
Another hint: note that the there are always at least two files: the directory and the model(s) inside. But you cannot see that if there is only one model version.
Model.xcdatamodeld
Model.xcdatamodel
Make sure that both files in the Finder have the same name before adding the directory to your project. (The directory will show up as a single file.)
We are making a common library in c++ for database and file reading.
We have decided a common folder structure for placing the files.
For eg. Insert statement files will be in a Lib/Files/Insert/
I will pass root directory of the project to c++ library, the library then will automatically append the path of the file to root directory.
For eg. If I need insert file of Author, I will pass /path/to/root/ to library. The library will append the rest of the path and pick up file like this: /path/to/root/Lib/Files/Insert/Author.txt
I was not able to open the file from c++ code.
Then I tried this to get the path of file using Objective-C:
NSString * authorPath = [[NSBundle mainBundle] pathForResource:#"Author" ofType:#"txt"];
It is showing the following path:
/Users/homam/Library/Application Support/iPhone Simulator/7.0.3/Applications/9C882381-DAA7-464D-AA84-8DB9288AAD71/CoreLibraryDemo.app/Author.txt
Although I have placed it in the folder /Lib/Files/Insert/, it is not included in the above path before Author.txt
So how can I maintain folder structure in iOS and read file from c++ library.
Are there any negative implications of excluding the file extension when using [UIImage imageWithContentsOfFile:] or is it best to include the file type in the path?
The Apple Docs state that initWithContentsOfFile: requires the following path as its parameter:
The path to the file. This path should include the filename extension that identifies the type of the image data.
However, the path for [UIImage imageWithContentsOfFile:] just states:
The full or partial path to the file.
I was just wondering if it's still best practice to include the file extension or to not even worry about it. Thanks!
You should include the file extension for all file requests. The only instance I can think of where you would not is using the
NSBundle pathForResource:ofType:
method. That method you specify the file extension outside of the file name. I'm relatively certain the "partial path" refers to relative paths. You can ask for a file in the root of your bundle by just the file name as it's path relative to your bundle is in the root, in other words just the file name.
The best way is to use NSBundle pathForResource:ofType:
If you have those images:
"myImage.png" (iPhone)
"myImage#2x.png" (iPhone Retina)
And you want to load it to an UIImage, you will use this code:
UIImage *myImage;
myImage = [UIImage imageWithContentsOfFile:[NSBundle pathForResource:#"myImage" ofType:#"png"]];
This code will also load the "myImage#2x.png" in iPhone Retina with no need to add the #2x to the code.
Be sure to write the exact file name. "myimage" might not work on device since the original file name has a Uppercase "I" ("myImage.png").
I hard code all URLs in my mobile app which I know is not a good idea. For example, if I were to ever need to move domain names I'd need to rename all URLs within the app from www.oldname.com to www.newname.com on each and every page.
I'm not sure where and how to store them so that I can modify this information on a single page and have it automatically changed across all pages.
In PHP I'd create a single file with variables that'd I'd include in all pages. I'm not sure how this is done in objective-c however. How do you do it?
You can create a constants file and include that in all other file where the URL is listed.
Create a file, e.g. constants.h where you put all your contstants.
In constants.h you could put something like this:
#define kApiBaseUrl #"https://myapi.mydomain.com"
And in your other files you acces it like this example:
NSURL *url = [NSURL URLWithString:kApiBaseUrl];
Probably this is like what you did use to do in PHP.
You can also set predefined URL's in the PLIST of your app but this is probably easier.
similar to edwardmp's response, you can also create a Constants.h file and define some static constant strings.
static NSString *const kSiteRootURL = #"http://www.someurl.com";
Check the folder "Supporting Files" inside your project you should have a file named "nameOfYourApp-Prefix.pch" then you can define the root URL for exemple
#define ROOT_URL #"yourwebsite.com"
and you can access ROOT_URL from anywhere in your project
You can keep your URLs in a property list file (File => New file => Property ...). You will have something like "config.plist". Similar with other property file, it is key => value file where the value can be a string, an arrays, etc.
Use NSBundle to load the file in your app, like
NSString *path = [[NSBundle mainBundle] pathForResource:#"config" ofType:#"plist"];
NSDictionary *config = [[NSDictionary alloc] initWithContentsOfFile:path];
This will give a benefit where you don't have to re-compile again your codes whenever you make any change on the URLs.
You can add a NSString to your .pch file to be included in all of modules.
Say for example In Xcode I make 3 folders in my supporting files older named "a" "b" and "c".
In each of these folders I have an xml file name "file.xml".
How can I use NSMainBundle to get paths to these 3 different xml files?
To get these files at runtime Xcode will copy them in the Copy Bundle Resource phase. This normally copies into the root of the bundle. To deal with directories see #CocoaFu's answer to this SO question.
Then in the code
NSBundle* bundle = [NSBundle mainBundle] will give you the main bundle
From this you look in directories using pathForResource:ofType:inDirectory: e.g.
NSString* path = [bundle pathForResource:#"file.xml"
ofType:nil
inDirectory:#"a"];
The methods are given in NSBundle class reference also see the Bundle Programming guide