views:

61

answers:

3

Whenever I do:

xxx = [NSImage imageNamed:@"Package.png"];

xxx loads but it's width and height remain 0. And whenever I try loading it into an NSImageCell I get this error:

NSImageCell's object value must be an NSImage.

Can someone help me out? I've never had this problem before.

Edit: Sorry, I've missed this bit out. So when I do it in the data source delegate it does not work and it shows the above error after 'return cell;'.

NSImageCell* cell = [[NSImageCell alloc] init];
[cell setObjectValue:xxx]; // Using imageNamed doesn't help either

Edit 2: This is becoming aggravating. I don't understand what happened but the image loads height and width properly, but it still complains when I add it to an NSImageCell.

alt text

+1  A: 

My guess is it's returning nil, in which case getting the width/height will return 0.

Try:

xxx = [NSImage imageNamed:@"Package"]; // the extension is not needed on the desktop or iOS 4

and make sure the image is actually being copied into your application's Resources folder!

Wevah
Same thing happens :(
Nick Brooks
Nick Brooks: The way to prove this theory would be to log either the pointer value (`%p`) or description (`%@`) of `xxx`. `nil` is the pointer 0x0, and `%@` will represent a description of `nil` (such as returned from a `description` message sent to `nil`) as “(null)”.
Peter Hosey
The pointer points to an NSImage object which is valid. However width and height are both 0. And I can't use it in an NSTableView.
Nick Brooks
Nick Brooks: NSTableView is irrelevant at this point; an image whose width and/or height is zero is not useful for anything.
Peter Hosey
A: 

Having established that you are, in fact, receiving an NSImage instance (not nil) whose width and height are zero, the next step is to determine why that happens.

  1. Can you open Package.png in Preview?
    1. If so: What's its width and height in Preview's Info window? Does it have any resolution (DPI or pixels-per-meter) information? If so, what does that say?
    2. If you crack open your application bundle, can you open that copy of Package.png in Preview?
  2. When you log the description of the image, what's the output?
  3. What's the output of logging the image's representations array? (This should be included in the image's own description, but I include it explicitly in case not.)
  4. What happens if you delete your build folder and re-build?
Peter Hosey
1. Yes - Yes2. [see next commment]3. There is one item which is the image itself4. Nothing changes after cleaning
Nick Brooks
<NSImage 0x10041db20 Name=Package Size={16.7989, 16.7989} Reps=( "NSBitmapImageRep 0x1004392c0 Size={16.7989, 16.7989} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=35x35 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x100437700"
Nick Brooks
That says the image does have non-zero width and height. Please edit the code that obtains the width and height into your question.
Peter Hosey
Ah, never mind—I spotted the problem in your screenshot. See my other answer.
Peter Hosey
+2  A: 

I see—so the table view aspect is relevant after all.

As the data source, your job is to return (and receive, in the case of user editing) the object values for the cells in the columns.

But you're not returning such a value; you're returning a cell. Thus, you're trying to set an image cell (created by the data source) as the value of an image cell (the existing one owned by the column).

The log message suggests that you have already set the column's cell as an image cell when you created the column, so all you need to do now is change your data source to always return the object value for the column, not a cell. For an image column, return the image. For a text column, return the string.

Note that NSTableView does not work like UITableView, where UITableViewCells are UIViews and you have as many cells as rows on the screen; in NSTableView, each NSTableColumn gets one and only one data cell, and that one cell is used to draw that column of every row. The cell draws its object value, which you provide to the cell, which you do by returning it (the object value) from your data source method.

The documentation about controls (an NSTableView is a kind of NSControl) and their cells is the Control and Cell Programming Guide.

Peter Hosey
Thanks for help. I've had a massive break from Cocoa by switching over to iPhone development. Didn't realize it had to return a value, not a cell.
Nick Brooks
Aha, good catch!
Wevah