Texture atlas either not found or resource missing images used - objective-c

I've just started using a texture atlas. It worked just fine up until I attempted to change the name from one with an uppercase letter to the same with a lowercase letter. Since then Xcode has been unable to find my atlases. Either that or when it can it loads the missing resource image rather than the one it should use.
I've deleted derived data, restarted Xcode and the Mac. Deleted and recreated the atlas folders in Xcode. Nothing seems to make a difference thus far.
Here's the code where I load the assets, just in case I've messed something up in it.
NSMutableArray *animationFrames = [NSMutableArray array];
SKTextureAtlas *animationAtlas = [SKTextureAtlas atlasNamed:#"animationImages"];
for (int i = 1; i < animationAtlas.textureNames.count; i++) {
NSString *texture = [NSString stringWithFormat:#"animationImage%02d", i];
[animationFrames addObject:[animationAtlas textureNamed:texture]];
}
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithTexture:[animationFrames firstObject]];

If you just changed the case of the name, it's probably not a texture atlas problem. Depending on how you renamed the resource, the Xcode name likely differs from the name on disk. If you added them as references to the project and not copied actual images, this is likely the issue. If they were copied (as in the group is backed by a folder on disk), it's likely a naming typo somewhere. You said it's still loading resources that are not there, which points to a dirty cache copy - make totally sure you uninstall the app from the device if that is the case.

Note that on iOS the file system is case sensitive. You can actually have two files with the same name differing only in case, for example:
animationImages.png
animationimages.png
So if you changed the case of a filename in your project, you have to change the corresponding code that loads this file even if the rename only changed the file's casing.
Also when replacing files, be sure to delete the app from the device and perform a Project -> Clean. Xcode only adds files to the bundle, it will never remove them, so your code may still inadvertently access the old file even though it's no longer in the project.

Related

Cocoa messes path up?

I'm trying to create an app that has an todays widget extension. This app should simply access a folder full of pictures, load them and draw them to the screen. One picture every hour. The widget should do the same, widget wise.
While loading an image in the main app and drawing it to the screen works flawlessly doing this:
NSString *tildePath = #"~/Documents/Adrian/Art/desertMovie.jpg";
NSString *path = [tildePath stringByExpandingTildeInPath];
_image = [[NSImage alloc] initWithContentsOfFile:path];
the extension completely messes up the path. It uses the same code as the main application but obscures the path completely.
From:
~/Documents/Adrian/Art/desertMovie.jpg
to:
/Users/Adrian/Library/Containers/ac.at.hulala.AK.ImageViewer2.ImageViewerWidget/Data/Documents/Adrian/Art/desertMovie.jpg
If I use a path without "~" the path stays the same but the image still doesn't get loaded.
Can someone tell my what I have to do that it accesses the same folder the main application does?
EDIT:
Archiving the app and exporting it seems to fix that problem. However I still would like to know how to make this working while developing.
EDIT2:
This is so weird! When working with the Todays Extension Widget a path like #"~/Pictures would lead to /Users/Me/Libraries/Containers/ac.at.ImageViewerWidget/Data/ like what the f***, why!?
When I force it to use the path #"/Users/Me/Documents/Pictures and run it the Memory Usage steadily increases until it reaches the limit and my system goes to hell. I really don't get that. I guess that the widget isn't finding any files so it tries to load the whole ssd. I guess. I really don't know whats the problem here.
Can someone please lead me into the light?
Not that xcode would tell you if something is wrong with your app. It will just allocate your whole memory and burn your cpu,
UNTIL,
you realize that the problem is caused by the lack of folder accessing permissions. Geezuz!
Add folder access permissions to your application if you want to access something!

What is the application Bundle and Resources folder

My question is about the application bundle in a project. I was reading about that and can understand some basic things (I'm not a native english speaker). I can understand that the resources folder is used to hold the files that will be used in the project, e.g. media files (images, audio, video, etc.) and should be in the application bundle to be identified.
So, what is the point if I want to use images and another resources in my project? In my other related question, I can't use them by referencing with NSImage imageNamed:.
I have used the following with no success loading my files:
NSBundle methods
imageNamed:#"string" with/without file extension
the images are in resources folder
I'm learning Cocoa and Objective-C, and of course this is different to C++ or Java when I want to create an ImageIcon or a QImage.
I may not have completely understood the issue, so correct me if I am wrong. I believe the problem has to do with your image's target membership or how you're retrieving the image in your code.
Adding an image to your project target will appropriately copy the resource at compile time. To add the image to your target, select it in the file navigator and then reveal the Utilities Panel. On the Utilities Panel, select the File Inspector Tab. Look for the Target Membership section and ensure that the image is selected for the desired targets:
Do you mean that you can't use the NSImage imageNamed: method to retrieve resources? If so, you can retrieve the resource like this (from the main resource bundle):
NSString *imageName = [[NSBundle mainBundle] pathForResource:#"image1" ofType:#"png"];
NSImage *imageObj = [[NSImage alloc] initWithContentsOfFile:imageName];
It also looks like you already have a good answer to your other related question.

Compile time check for valid file references in Xcode

Is it possible to force the Xcode complier to verify that files referenced in code are valid?
There are multiple points in Cocoa development when you naturally reference a file programmatically via an NSString:
[UINib nibWithNibName:#"MyNib" bundle:nil];
[UIImage imageNamed:#"MyImage"];
[[UIViewController alloc] initWithNibName:#"MyNib" bundle:nil];
Is there any way at compile time to check is these file references are valid?
Often times after using above methods, I end up changing the name of the referenced file but forget to change the name in code. Everything complies without a problem and it is only when you happen to go to the portion of the app that accesses this file that the bug will reveal itself.
Is there another approach or technique that people use to avoid this sort of error?
Referencing a file name via a string feels very fragile.
Warning: This answer is mostly outdated. The general idea is fine but better solutions exist now (e.g. Image assets with a SwiftGen script to generate an enum).
Nibs usually have a class with the same name as the file, e.g.
[[MyViewController alloc] initWithNibName:NSStringFromClassName([MyViewController class]) bundle:nil];
I usually hide it into the controller's init method as [self class].
For image loading, compile-time checks are difficult. Help yourself with macros, first replace the loading method by a simple macro, e.g.
#define LOAD_IMAGE(__IMAGE_NAME__) [UIImage imageNamed:__IMAGE_NAME__]
First thing you should do is to put an assert into this macro and always check that the image was successfully loaded. It's not a compile-time check but it helps to find missing resources.
The second thing is to write a ruby/python/shell/(any scripting language) script that will search your source files for LOAD_IMAGE and check if the file (between parenthesis) exists. As a shell script, it will be very simple (e.g. using grep). You can add this script into your xcode project to be run when compiling.
Don't forget to check images referenced by xibs.
However, often you have to create the image name dynamically, e.g. NSString* imageName = [NSString stringWithFormat:#"image_%i", index]. There's no way how you can check this at compile time.
Also don't forget to do the reverse check - don't include image files which are not used anywhere.
AutoComplete for [UIImage imageNamed:] by Kent Sutherland.
This provides code completion support within Xcode - a brilliant piece of code. This is working for me in Xcode 4.6:
Currently this project does not have support for strings other than imageNamed:. To support those, I will try to write a compile time script. Or maybe I will become bold and try to extend Mr. Sutherland's spectacular work.
Xcode doesn't support this, but if this problem is really biting you then you could use the following hack:
Give every in-bundle file a unique prefix (e.g. app__)
When you add a file to your project, make sure you first rename it to add this prefix.
Your compile time (pre-distribution) check then has two parts: 1) Search through all .m files and enumerate strings that begin with the prefix. You shouldn't have to check if the string is quoted since your prefix is unique. 2) grep project.pbxproj for each string to check if it is included in the bundle.
With some effort, this process can be mostly automated and also optimized, but the above recipe ought to work.
here is a bash script that we use that lists all images on disk but NOT referenced in code.
https://gist.github.com/3750087
it would likely be easy to reverse this to check for non-exting images and xibs.
Anyways, the script should be a good starting point

Clearing .png files from a Xcode project

In my Xcode project, i added a bunch of images to use. I need to change out the images so i removed them from the project and deleted them from the folder structure. They should be gone, right?
No. when i launch the program, the images are still being used. I did a search on my computer for the file name and only found it in the code
(i used a constants file and put the file names into strings:
NSString * const PRESET_LIGHT_GRAY = #"preset-rect-16.png";
NSString * const PRESET_ASH_GRAY = #"preset-rect-17.png";
and this is the only place on my mac that i find the file name).
there is no file on my mac named "preset-rect-16.png" yet Xcode is still using the image.
where is Xcode getting the images? are they stored somewhere else in the project? are they saved in the simulator somewhere?
/puzzled
Go here. Xcode -> Preferences -> Locations -> Derived Data
Wherever your Derived Data Folder is, delete Build for this Project and then run again. Resetting the Simulator is also recommended.
The images may still be referenced in your target build phases under copy bundle resources. If so delete the references there as well.

Array Doesn't load PNGs Exported from After Effects: ios, xcode

Our graphic designer is sending us .PNGs named appropiately "hide_00~iphone.png", "hide_00#2X~iphone.png" etc
He is exporting the images from after effects. I add them to the project and try to load them into an array on init. An exception is thrown each time for all of his files. Now, if I go in AND RENAME the files in the finder to exactly the same name, everything compiles fine. I have no idea what's going on here. Xcode cannot find them in the file system until I rename them. But the name is EXACTLY the same as what he sent me. I checked for white space around his file naming but everything looks fine.
Does anybody know if After Effects puts weird header info in the images? Or does this sound familiar at all to anyone. There are a whole bunch of images we are working with and I would hate to have to rename them by hand.
So I just used imageNamed and the image loaded just fine. So obviously your routine to load the image by path/name is flawed. You can post that and it can be evaluated - but this has nothing to do with 'After Effects'.
EDIT: For the record, my system is Lion Xcode 4.4.1 and my project set for iOS 5.1. I took your file from dropbox, and verified that in my project I CAN load the image as you are trying to do:
for (int i = 6; i < 7; i++) {
NSString *path = [NSString stringWithFormat:#"hide_step_seq_%02d", i];
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:path ofType:#"png"]];
NSLog(#"PATH %# image=%#", path, image);
}
2012-09-13 07:15:23.241 Searcher[58114:f803] PATH hide_step_seq_06 image=<UIImage: 0x6a4cb30>
So, where to go from here? I've tried to help several people here who get burned by the #2x or ~iphone suffixes. For some reason a few people cannot ever seem to get this to work - all I can think of is there is some flag deep in the system that gets toggled and there is no way to untoggle it.
My suggestion is to try using the actual complete file name - try appending ~iphone and see if that works.
You can also in Terminal do a 'ls *.png | od -c' before changing the name and afterwards, to verify that absolutely the characters are the same.
The last thought I have on this is that files have many attributes: creation time, last access, last modiied, extended attributes, permissions, etc. It is possible (while unlikely) that for some reason one of these values blocks the system from attempting to use the ~iphone suffix.
I really wish I could help you further. If you want to put a simple little project together that does nothing more than tries to open a few images and it fails, zip the whole project up, put on dropbox, I'd be more than willing to run it on my system to try and duplicate the problem. You can also do as I did in the code above and verify that path looks good and the image is nil.