开发者

Can't create an NSImage?

开发者 https://www.devze.com 2023-01-12 08:40 出处:网络
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:

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 e开发者_运维知识库ither

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.

Can't create an NSImage?


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.


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!


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?
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号