Using Interface Builder's System Media files in code - objective-c

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

Related

How can I change the value of an NSImage?

I am a beginning Objective-C programming programmer, and I want to change the value of an NSImage. That means that if I have an NSImage that is a NSStatusUnavaliable, how could I make it a NSStatusAvaliable? Thanks!
So I believe what you're asking is: Somewhere you have an NSImage instance that was initialized like:
NSImage *image = [NSImage imageNamed: #"NSStatusUnavailable"];
And you would like to use the image that is mapped to #"NSStatusAvailable".
Without going into details, you really want to assign your image to [NSImage imageNamed: #"NSStatusAvailable"];
Take a look at the docs for NSImage. Specifically what you might find useful is the imageNamed: class method.
Discussion
This method searches for named images in several places, returning the first image it finds matching the given name. The order of the search is as follows:
Search for an object whose name was set explicitly using the setName: method and currently >resides in the image cache.
Search the app's main bundle for a file whose name matches the specified string. (For information on how the bundle is searched, see ““Accessing a Bundle's Contents”“ in >Bundle Programming Guide.)
Search the Application Kit framework for a shared image with the specified name.
But also since you're new, just reviewing Objective-C docs on object life cycle will be very useful to understand what's going on.
Swift:
let 🈯️ = NSImage(named: .statusAvailable)
let 🅿️ = NSImage(named: .statusPartiallyAvailable)
let 🔴 = NSImage(named: .statusUnavailable)
let 🔶 = NSImage(named: .statusNone)

Setting Path of UIImage

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

iOS - Loading an image from a web server in Objective C

At the moment, I have a goal of writing an application that contains a UIImageView. Upon loading or pressing a button, an image from a web server will load into the view.
What is the simplest way to do this? I'm assuming using the API described here? https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
In the mean time, I will continue to try to get this working on my own. Any advice would be appreciated, especially if you have written code like this before.
Yes, this can be done by making a request through an NSURLConnection.
You might be interested in checking out SDWebImage. This library includes a category that adds a method to UIImageView that makes asynchronously loading an image from a remote URL as simple as this:
[self.imageView setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:#"placeholder.png"]];

How to use localized UIImage with specific language?

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.

Changing an NSImage in XCode - this line of code not working

I am having a problem that is eating me alive. I really hope I am just missing something small here. It appears to be a rather "n00b" issue.
I have a blank NSImageView that I want to display a picture when a button is pressed — simple as that.
Here is my line of coding
NSBundle *mb = [NSBundle mainBundle];
NSString *fp = [mb pathForResource:#"tiles" ofType:#"PNG"];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:fp];
if ( [image isValid] ) {
[selection setImage:image];
[selection setImageScaling:NSScaleProportionally];
}
Whereas,
tiles.PNG is a resource in my bundle
and if [image isValid] is satisfied, because I've inserted dummy code into the clause and had that work
selection is defined in my header file as follows
IBOutlet NSImageView *selection;
It is also linked up to the application delegate in IB.
I have a feeling I might not be linking it properly?
WHy wouldn't the image display? If anyone can see an error - or provide me with working code - I would be soooooo thankful
Brian
It's not a linking issue—your app wouldn't even launch (assuming it even links successfully) if you'd failed to link against Cocoa or AppKit.
More probably, either you haven't connected the outlet to your image view in your nib, or you haven't loaded the nib yet. The way to check this would be to NSLog the value of the imageView pointer, using the %p formatter.
I had a similar issue where my view wasn't displaying, and it turned out that the view was hidden. This was a setting in the view properties in Interface Builder. Just a punt, but give it a go.
You need to use the debugger and see what's going on as it runs. Is fp nil? Is image nil? Is selection nil? The debugger is your friend.
did you remember to send -setNeedsDisplay to the NSImageView after you set the image?