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

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.

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.

Texture atlas either not found or resource missing images used

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.

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

How to use UTIs to make a new file type?

I have been working on making an application for editing game plugins for a game I made. I needed the files it saves to have a custom format for obvious reasons. Unfortunately, I cannot figure out how to give the file a custom icon.
I did some research last night into UTIs, mostly because they are mentioned wherever icons are talked about. I did what the Apple Documentation said, but it didn't work.
Here is my code so far:
[#"test" writeToFile:#"test.hsimg" atomically:YES encoding:NSUTF8StringEncoding error:NULL];
That works perfectly. The only problem I have now is that there is no icon. Could someone please tell me how I need to setup the UTIs to make it work?
Take a look at these questions:
How do I associate file types with an iPhone application?
Creating my own file extension based on plist