Adding an Image in Xcode 4.5 - MacRumors Forums
Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 14, 2012, 08:33 PM   #1
Blakeasd
macrumors 6502a
 
Join Date: Dec 2009
Adding an Image in Xcode 4.5

Hello,

This is my first time writing/updating an application since ~ the beta version of OS X Lion. I am trying to add an image to an NSView in interface building. Here are the steps that I followed:

1. Dragged images to Xcode "Supporting Files" Group (Copy was checked)
2. Went to the interface building section, pressed on the media tab and dragged the image to the NSView

Presses the Run button and... got an error stating that Xcode "Could not find the imaged named" [image name]

What has changed that won't allow me to add an image? I noticed a similar thing with adding new classes, I have to target the .m file in the inspector or Xcode cannot find it. The image under the inspector is check under the "Target Membership" table in the inspector. Could someone please help me?

Thanks!
Blakeasd is offline   0 Reply With Quote
Old Dec 15, 2012, 09:25 AM   #2
rien333
macrumors regular
 
Join Date: Jun 2010
Location: The Netherlands
What you need to do is the following:

1. Make a new subclass of NSView.
2. Add an NSImageView instance variable to it. (You know, in the newly made .h file)
3. Initialize the NSImageView in the init method of your NSView subclass, you know, the .m file.

It will look something like this:
Code:
//  MyImageView.m

#import "MyImageView.h"

@implementation ImageView

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        theImage = [[NSImage alloc] initWithContentsOfFile:@"Your Image"];

        theImageView = [[NSImageView alloc] initWithFrame:[self bounds]];   //Doesn't necessarily have to be this value 
        [theImageView setImageScaling:NSScaleToFit]; // Same goes for this one
        
        [theImageView setImage:theImage];
        [self addSubview:theImageView];
    }
    
    return self;
}

- (void)drawRect:(NSRect)dirtyRect {

    // Add drawing code, if necessary. (Don't think it is for you)
}

@end
3. Now, in Interface Builder select your custom view for your image.
4. Select the Identity Inspector, and set it's class to "MyImageView" (or whatever name you gave it)
5. Build and Run your project.

Feel free to ask more, if needed.
__________________
Macrumors newbie, happy to be here! (And sorry if any of my English is incorrect)
rien333 is offline   0 Reply With Quote
Old Dec 15, 2012, 09:34 AM   #3
Blakeasd
Thread Starter
macrumors 6502a
 
Join Date: Dec 2009
Thanks!
Blakeasd is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Xcode gets the wrong path when adding files printz Mac Programming 1 Mar 6, 2014 04:01 AM
Help adding stuff to my xcode project Ltaa09 iPhone/iPad Programming 4 Sep 23, 2013 11:42 PM
Adding a header file to Xcode n00blar Mac Programming 3 Dec 20, 2012 07:57 PM
Adding an Image in Xcode 4.5 Blakeasd OS X 10.8 Mountain Lion 1 Dec 14, 2012 08:32 PM
Help with adding and subtraction in Xcode kingv84 Mac Programming 5 Jun 18, 2012 04:36 PM

Forum Jump

All times are GMT -5. The time now is 08:16 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC