I'm having trouble adding NSsound instances into an NSMutableArray
The error I get is that I cannot do [soundFiles addObject:soundObj], because soundObj is nil. But if I do [soundObj play], it will play - so it is an instance.
//Name of all the sounds to load in.
sounds = [NSArray arrayWithObjects:
#"sound1",
#"sound2",
nil];
for (int i = 0; i < sounds.count; i++) {
NSString *soundName = [NSString stringWithString:sounds[i]];
NSSound *soundObj = [NSSound soundNamed:soundName];
[soundFiles addObject:soundObj];
}
If I change [soundFiles addObject:soundObj] to [soundFiles addObject:soundName] it is fine, so it is something with trying to pass the NSSound
I'm hoping to be able to preload a bunch of very short sounds to make them play the minute they are called. milliseconds matter for this project.
I was hoping this would allow me to do [[soundFiles objectAtIndex:1] play] - as I was thinking that would result it being faster than creating the NSSound object when it is time to play it.
You've probably figured this out by now, but my guess is that you were writing the code before you had created ALL the sound files you wanted to work with. So when you tried to create an NSSound instance of those nonexistent files, it got set to nil. It's hard to troubleshoot because NSSound doesn't throw an error when you point it to a non-existent file.
I had the identical problem until I created all the sound files I wanted to use. I just wanted to post my answer to help anyone else who's having this problem because a Google search turns up nothing that helps.
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 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
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.
I just discovered the NSRect helper functions in NSGeometry.h (i.e. NSMidX, NSMaxX, etc...)
These would have made some repetitive coding much easier. I knew about NSMakeRect, NSMouseInRect, NSOffsetRect and many others but somehow missed the functions that aid in recalculating NSRect geometry.
I've found NSStringFrom*() helpful when logging structs like CGRect, CGPoint, etc.
You can find a comprehensive overview at Apple's Foundation Functions Reference (Wayback Machine link).
Helper function to draw three part images with left cap, fill and right cap. Ideal for custom buttons
void NSDrawThreePartImage(NSRect frame,
NSImage *startCap,
NSImage *centerFill,
NSImage *endCap,
BOOL vertical,
NSCompositingOperation op,
CGFloat alphaFraction,
BOOL flipped
);
Also look for NSDrawNinePartImage
This is one that I wish I had known about 6 months ago. I was creating our first iPhone application and I wanted to create a simple help file that was based on HTML using the UIWebView Controller.
However I could not figure out how to embed local images that I had stored in the Bundle and I did not want the user to have to have internet access to fetch the images from a server.
Little did I know I could do the following to get images from the Main Bundle
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSURL *bundleBaseURL = [NSURL fileURLWithPath: bundlePath];
[webView loadHTMLString:htmlContent baseURL: bundleBaseURL];
The Image in your HTML can then call local images directly.
<img src="yourImageFromTheMainBundle.jpg" />
I had no idea I could set the baseURL with the location of the Bundle.
Much of the stuff in NSPathUtilities.h. I did know about it two years ago, but when I first found it I wished I’d seen it two years earlier. :-)
At some point I wasted quite a bit of time because I didn’t know about NSCountedSet, and made a mess of my dictionary-based replacement. I know of several cases where people have done the same sort of thing because they didn’t know about NSSet at all. Another good “hidden” collection is CFBinaryHeap, which implements a priority queue, but doesn’t have an NS equivalent.
One I remember
+ (NSBezierPath *)bezierPathWithRoundedRect:(NSRect)rect xRadius:(CGFloat)xRadius yRadius:(CGFloat)yRadius
Granted figuring out how to draw a rounded rectangle manually is a pretty good exercise. There are others that I am just so used to now.
Creating a colour from a pattern image with
[UIColor colorWithPatternImage:[UIImage imageNamed:#"mypattern.png"]];
This is a shortcut instead of a library call that I missed, but it is in the spirit of the thread.
One shortcut that I use alot is using an inline format statement in NSLog calls.
NSLog(#"x=%#", [someobject className]);
instead of the more verbose
NSLog([NSString stringWithFormat:#"x=%#", [someobject classname]]);