13 Replies Latest reply: Feb 3, 2013 4:14 PM by MacMan240
MacMan240 Level 1 Level 1 (80 points)

I am making a Cocoa Application I would Like to get an image to be in the background without using an image well.

Screen Shot 2013-02-03 at 1.07.05 PM.png


MacBook Pro, OS X Mountain Lion (10.8.2), 2.3 GHz i5, 4GB RAM, OCZ 120GB SSD
  • xnav Level 5 Level 5 (6,630 points)

    You could use a WebView that just loads a jpeg.

  • red_menace Level 6 Level 6 (14,760 points)

    You can also directly set the window background to an image, depending on whether your window is resizable or if the image is some kind of pattern.  The image will be tiled if it is smaller than the window, so if the window is a fixed size and the image is not a pattern you can to resize it to match the window.

     

    The following example uses an outlet property that is connected to the main window (not the view), so place it with the rest of your outlet properties:

     

    property mainWindow : missing value -- outlet connected to the main window

     

    These statements should be placed somewhere with the rest of your initialization code, perhaps in the applicationWillFinishLaunching handler.  I am also guessing that your image will be in the Resources folder of the application bundle (make sure you have it added to the Copy Bundle Resources build phase):

     

    set myBundle to current application's NSBundle's mainBundle()

    set backgroundImage to current application's NSImage's alloc's initWithContentsOfFile_(myBundle's pathForResource_ofType_("image name", "jpg"))

    backgroundImage's setSize_({350, 230}) -- match window size

    mainWindow's setBackgroundColor_(current application's NSColor's colorWithPatternImage_(backgroundImage))


  • MacMan240 Level 1 Level 1 (80 points)

    where do I copy the picture during the build?

  • MacMan240 Level 1 Level 1 (80 points)

    got error:

    -[AppDelegate applicationWillFinishLaunching:]: The variable mainWindow is not defined. (error -2753)

     

     

    on applicationWillFinishLaunching_(aNotification)

      -- Insert code here to initialize your application before any files are opened

            set myBundle to current application's NSBundle's mainBundle()

            set backgroundImage to current application's NSImage's alloc's initWithContentsOfFile_(myBundle's pathForResource_ofType_("plex", "png"))

            backgroundImage's setSize_({350, 230}) -- match window size

            mainWindow's setBackgroundColor_(current application's NSColor's colorWithPatternImage_(backgroundImage))

              end applicationWillFinishLaunching_

  • red_menace Level 6 Level 6 (14,760 points)

    To add the image to your project, right click in one of your group folders (Supporting Files, for example), select the Add Files to... menu item and choose the file.  It should also appear in the Copy Bundle Resources phase in the project Build Phases.

  • red_menace Level 6 Level 6 (14,760 points)

    You need to add the property declaration and connect it to the main window.

  • MacMan240 Level 1 Level 1 (80 points)

    Ok, sorry for all of the trouble in the muliple threads but I'm obviously horrible and new to this, what do I put in place of "missing value" to connect the property declaration to the main window?

  • red_menace Level 6 Level 6 (14,760 points)

    In the Interface Editor, if you select the blue cube that represents your AppDelegate instance and look at the Connections Inspector, all of the Outlets and Received Actions will be listed.  When you add a property that is set to missing value (it is just a placeholder), it will show up in the outlets, just as a handler with a single parameter will show up in the received actions.

     

    The various UI objects are connected to outlet properties in the same way you connect to the buttons - once connected, the property is used as a reference to the UI object (things get connected up and the property is given an actual value the application is run).

  • MacMan240 Level 1 Level 1 (80 points)

    Ahhh, I got it, thank you for your help and patience, I don't want the image however to be the size of the window, but I dont want it to tile either, is there a way to subltly(medium in size, maybe off to one side) have it in the backround of the window?

     

    Message was edited by: MacMan240

  • red_menace Level 6 Level 6 (14,760 points)

    Make sure you connected the property to the window and not the view - errors will get logged to Xcode's debug console, so you can check to see what is (not) happening.

  • MacMan240 Level 1 Level 1 (80 points)

    Ahhh, I got it, thank you for your help and patience, I don't want the image however to be the size of the window, but I dont want it to tile either, is there a way to subltly(medium in size, maybe off to one side) have it in the backround of the window?

  • red_menace Level 6 Level 6 (14,760 points)

    I chose the pattern image since it is fairly simple.  To get more control over the background, you would be looking at subclassing NSView or adding some view layers in there.

  • MacMan240 Level 1 Level 1 (80 points)

    I decided to use photoshop to make a background the exact size and then can make it exactly as I like from there, not sure why I didn't think of that before. Thank you very much for your persistant help!!