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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 06:46 PM.

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

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