views:

744

answers:

3

Here's my code so far (in the draw rect):

// Drawing code here.
NSLog(@"%@", [[NSBundle mainBundle] pathForResource:@"NoiseBGMainView" ofType:@"jpg"]);
NSURL *pathToBGImage = [[NSURL alloc] initWithString:[[NSBundle mainBundle] pathForResource:@"NoiseBGMainView" ofType:@"jpg"]];
NSImage *NoiseBGMainView = [[NSImage alloc] initWithContentsOfURL:pathToBGImage];
[NoiseBGMainView drawInRect:[self bounds] fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];

And the image doesn't draw... I don't know what's wrong.

+1  A: 

Why not just use [NSImage imageNamed:@"NoiseBGMainView.jpg"] instead? That will give you an NSImage of your picture (if it can find it), and nil if it can't. NSLog what the results of that call are, then come and report back on whether it's logging (null) or not.

Dave DeLong
The path is all fine, its the drawing that is the problem
nanochrome
Was just going to answer this. +1
Jacob Relkin
Well, even if the path is ok, using `imageNamed:` is better because the code you show is leaking an NSURL and NSImage each time it runs.
Kelan
Also worth mentioning that it's not good practice to name instances with an initial capital letter. The image variable should be named `noiseBGMainView` or preferably `noiseBGMainImage` since it's an image, not a view. See http://developer.apple.com/Mac/library/documentation/Cocoa/Conceptual/CodingGuidelines/
Rob Keniger
Yeah I know that.... I coded this late at night :P
nanochrome
Also I'm trying to ask what's wrong with drawing....
nanochrome
A: 

You can draw an NSImage into an NSView without writing any drawing code by attaching an NSImageView to your view and setting your image resource as the NSImageView's image. (NSImageView handles all the drawing).

If your custom NSView is loaded from a nib, you don't need any setup code either - just make the appropriate NSView-NSImageView-NSImage (resource) connections with Interface Builder.

tedge
I Have this, and this doesn't work: `NSImage *noiseBGMainView = [NSImage imageNamed:[[NSBundle mainBundle] pathForResource:@"NoiseBGMainView" ofType:@"jpg"]]; NSLog(@"%@", noiseBGMainView); NSImageView *bgImageView = [[NSImageView alloc] init]; [bgImageView setImage:noiseBGMainView]; [self addSubview:bgImageView];`Any Idea What's Wrong?
nanochrome
If you set up NSImageView programmatically (instead of with IB), you'll need to give it a nonzero frame.Try changing:NSImageView *bgImageView = [[NSImageView alloc] init];to:NSImageView *bgImageView = [[NSImageView alloc] initWithFrame: NSMakeRect(0,0,100,100)];
tedge
Works great, but I need the image to repeat?
nanochrome
+1  A: 
NSURL *pathToBGImage = [[NSURL alloc] initWithString:[[NSBundle mainBundle] pathForResource:@"NoiseBGMainView" ofType:@"jpg"]];

This is wrong. -[NSURL initWithString:] takes a string version of a URL, such as http://apple.com/, not a pathname.

Because you didn't pass a URL, the URL will be a relative URL, but a relative URL needs a base. The URL is a valid object, but doesn't lead anywhere. This, in turn, causes initWithContentsOfURL: to return nil (because you tried to load an image from a relative URL with no base). You then try to tell the image to draw, but you don't have an image, so nothing happens.

You want initFileURLWithPath:. (Or the class method fileURLWithPath:, with which you won't have to release the URL later.)

Peter Hosey