I would like to know how to set the path (into folders) of a UIImage when I'm doing something like this:
[UIImage imageNamed:#"Sprites/Macbeth/Climbing/1.png"];
I want to to have it use the image entitled "1.png" in the folder Sprite, in that folder in Macbeth and in that folder Climbing. I want to be able to do that with one line of code since I have a lot of UIImages like that for sprite animations (all in arrays). Does anyone know how to do this? Thanks in advance!
iOS does not maintain the folder system you have. So this isn't possible. You would pull your image simply using this line of code.
[UIImage imageNamed:#"1.png"];
Instead of trying to nest folders try putting that into the file name like macbeth-climbing-1.png if you need to classify multiple 1.png's
Write a method or function that does the following:
Call [NSBundle mainBundle] to get your application bundle
Call -[NSBundle pathForResource:ofType:inDirectory:] to get the path to the image
Call +[UIImage imageWithContentsOfFile:] with that path
Related
I want to load multiple images when my app starts from a website (i.e. all images in http://hello.com/images/ which are named 1.png, 2.png, 3.png..) , so that the images can be used anywhere in the program without needing to reload them every time I want to access them.
Can I simply create a class that holds a static NSArray and fill it at the beginning, to then create an instance of this class whenever I need the images or is there a better way to do it?
Right now, I am loading the images with the following code:
UIImage *image =[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://hello.com/images/%#.png,item]]]];
I want to make the app as efficient as possible, so I am concerned about the creation of multiple objects making it very demanding.
Thanks
You can try downloading the images asynchronously in a separate thread when the application starts
and use it later.
Here is the SO question and answer where the poster uses a custom class to download the images in the background asynchronously.
Try this for efficient download of images and UI also will not be blocked.
I have settings in my App and want to give the user the possibility to switch the language of the app. The language can be different to the device setting.
For the NSStrings I found a solution but how can I do it with UIImages?
I've localized all my images but now they are chosen depending on the device language. What do I have to do to get this working? My only option that I have right now is to put the names in the Localizable.strings and load them from there.
Is there a better way to do this?
Thx ;-)
Something like:
- [NSBundle URLForResource:withExtension:subdirectory:localization:]
- [NSBundle pathForResource:ofType:inDirectory:forLocalization:]
should do it.
Then just use something like:
-[UIImage initWithContentsOfFile:]
-[UIImage initWithData:]
for creating a UIImage.
Is it possible to iterate through all the .png images that are resources in my iPhone game? Maybe having a filter to only find for me images that begin with a certain prefix...
I know it sounds weird. It is like considering I don't even know what images I got in my project. Well, this is more for experimentation purposes.
You use the appropriate NSBundle for this, if this is the main bundle, this would look like this:
NSArray *allPNGs = [[NSBundle mainBundle] pathsForResourcesOfType:#"png" inDirectory:nil];
You can then iterate through the array and check the prefixes of the returned files.
I have a question regarding the several images and sound files in the Media tab of IB's library: how can they be used in code?
I want change the image of a NSImageVew to NSRevealFreestandingTemplate, but how can I access it? I'm pretty sure there must be a better way than creating a hidden image view and pulling it from there. Any ideas?
Thank you!
Use NSImage's imageNamed: method to get system images. You can use any of the values found in NSImage's Constants section as the name. To get the image you specified, you would use:
[NSImage imageNamed: NSImageNameRevealFreestandingTemplate];
[imageView setImage:[NSImage imageNamed:#"NSRevealFreestandingTemplate"]];
I am trying put an image in a view. This is my code:
NSImageView *imView = [[NSImageView alloc] initWithFrame:NSMakeRect(0, 0, 480, 360)];
NSImage *myImage = [[NSImage alloc] initByReferencingFile:#"image.png"];
[imView setImage:myImage];
[theView addSubview:imView];
[window setContentView:theView];
But the window is blank(image doesn't get drawn). If I place a textfield in the view for example.. it works perfectly, but not NSImageView...I checked the console and there's no warning/error messages...
Thanks!
Since [imView isValid] returns false, this means that NSImage hasn’t been able to create an image based on that file. A common reason is that the file couldn’t been located. From the documentation of -initByReferencingFile:,
filename
A full or relative path name specifying the file with the desired image data. Relative paths must be relative to the current working directory.
This means that the behaviour of -initByReferencingFile: depends on the current directory, and ideally you should pass the full path name.
A better solution is to use +imageNamed: instead because of two reasons: it uses an image cache and it is able to look for a file inside the application bundle.