Can't get path of image in NSBundle? - objective-c

I am trying to get the path of an image while UITesting. I've tried multiple ways as shown under here, but none are working?
Name of image file is: TestImage.fff
NSBundle* testBundle = [NSBundle bundleForClass: [self class]];
NSString* testImage = [testBundle pathForResource:#"TestImage.fff" ofType:#"fff"];
NSString* testImage2 = [testBundle pathForImageResource:#"TestImage"];
NSURL* testImage3 = [testBundle URLForResource:#"TestImage" withExtension:#"fff"];
Can someone tell me where I am going wrong?

Inside XCode, select your test target inside the target list. Then select the Build Phases tab and inside the Copy Bundle Resources phase, add the required resources.

Related

using SRCROOT in Xcode

I am trying to use SRCROOT path in my project like so:
// Create NSTask to interact with console
NSTask *task;
task = [[NSTask alloc] init];
// Set path to script
NSString *launch_path = #"$SRCROOT/Scripts/my_script.rb";
NSLog(launch_path);
[task setLaunchPath:launch_path];
I end up with (in log):
$SRCROOT/Scripts/my_script.rb
I want to end up to a path inside my project's directory like this:
/Application/Scripts/my_script.rb
Can anyone help me understand what I'm doing wrong? Thanks in advance.
Additionally, I've tried the following without success:
..
// Set path to script
NSString *launch_path = [NSString stringWithFormat:#"%#/Scripts/auto_checkin.rb", "$SRCROOT"];
NSLog(launch_path);
..
$SRCROOT is meaningless for runtime environment.
If you want to access the document in bundle, make sure the my_script.rb is added in the "Copy Bundle Resources", then try this:
NSString * path = [[NSBundle mainBundle] bundlePath] ;
path = [path stringByAppendingPathComponent:#"my_script.rb"] ;
$SRCROOT is path to the project file(*.xcodeproj) that defines the target. link here
The bundle term here represent Application bundle, the application bundle stores everything that the application requires for successful operation. link here

How to tell if a file exists always returns false but I can load it via image tag

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.

How do I copy a directory that is inside of my main bundle to an NSPasteBoard?

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: #""];

Embedding XML to iOS application

I'm making my first game in iOS and I need to load some levels that are stored as XML. Level files are stored locally and everything works fine from emulator. However, since XML is loaded in the runtime when i try to test my game on an actual device it can't find the XML files since they are not actually part of the app.
I'm coming from Flash background so I might have a wrong idea how this is done on iOS but I need to somehow bundle those XML files with the app, or embed it in the code somehow? Is this possible?
Thanks a lot :)
Well The code to look up your app bundle for the specified file is
NSString *path = [[NSBundle mainBundle] pathForResource:#"fileName.xml" ofType:nil]
NSURL *xmlURL = [NSURL fileURLWithPath:path];
[[NSXMLParser alloc] initWithContentsOfURL: xmlURL]
Hope this helps you...
You could add the XML file to your project and run time read it up with something like this
NSArray *myPathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *myPath = [myPathList objectAtIndex:0];
NSError **err;
myPath = [myPath stringByAppendingPathComponent:fileName];
if([[NSFileManager defaultManager] fileExistsAtPath:myPath])
text = [NSString stringWithContentsOfFile:myPath encoding:NSUTF8StringEncoding error:err];
You could consider using JSON in instead of XML. At least to my knowledge the available XML parsers are not nearly as simply to use as SBJson for instance (https://github.com/stig/json-framework/)
Here is my solution in Swift 3.0. In addition to the code, you'll need to add your xml file as a data set in your Assets.xcassets. Do this by creating a new Data Set, giving it any name, and then dragging the xml file from the finder to your newly created data set. When you reference the file in your code, you'll use the file name of the file, and not the name of the data set.
var parseResults = false
if let path = Bundle.main.path(forResource: fileName, ofType: ".xml") {
let url = URL(fileURLWithPath: path)
if let xmlParser = XMLParser(contentsOf: urlToFile) {
xmlParser.delegate = self
parseResults = xmlParser.parse()
}
}

Folder in resources directory!

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.