views:

52

answers:

2

I was wondering if anyone could tell me if there is a method in Cocoa that will display information (images) on screen when a button is pressed. What I mean is NSLog "prints text" to the console is there a method that displays images just as easily like would -(void)drawView do it? Is it just setNeedsDisplay? I hope this makes sense. I am essentially wanting to know if I can call something that will display an image as easily as you can display/print text to the screen/console.

A: 

You should take a look at Apple's NSImageView Class Reference.

This a class you can use to display an image in Cocoa.

setNeedsDisplay is a NSView method that tells the graphics renderer it needs to redraw the image because the data has been modified. Presumably because you are using something like Quartz and you have called some custom drawing code. If you are drawing bitmap images then you probably won't need to use this.

Brock Woolf
Are you suggesting that one might use Quartz 2D to draw over a view, then use `setNeedsDisplay:` to wipe away the drawing? That's not good app design or good controller behavior; views should be fully capable of drawing themselves. If that's not what you're suggesting, then I don't know what you're suggesting.
Peter Hosey
(Come to think of it, whether you're using Quartz 2D or AppKit to draw is irrelevant. You can do either one outside of `drawRect:`, and you can and should do either one only inside of `drawRect:`.)
Peter Hosey
This is the best explanation of "setNeedsDisplay:" I have read. My question was poorly worded, but thanks for explaining what goes on for setNeedsDsiplay: by using an example of why it is useful. This "here is why it could be useful" information is sadly missing from the Apple docs. Both Brock and Peter have been helpful Thanks again. Can I check mark both?
I00I
You can only check one, but you can upvote both by pressing the up arrow
Brock Woolf
+1  A: 

The Console is text-only, so no, you can't print an image to it the same way you log text. The closest equivalent is to export the image as TIFF data and write that data to a file in the temporary directory.

As for setNeedsDisplay:, that tells AppKit that the view should be told to redraw the next time the window redraws its views. (In other words, it sets the view as needing display—exactly what it says on the box.) Usually, this is because you've changed the model object(s) that the view displays, either by replacing them with other objects or by mutating one or more of their properties.

You would need to have a view to display; an image view would certainly qualify, but if you're looking for the image equivalent to NSLog, this isn't it, unless you don't mind either making a dedicated window just for showing this image or temporarily putting a image view into one of your real windows.

Peter Hosey