无法创建 NSImage?

发布于 2024-09-14 22:55:34 字数 634 浏览 7 评论 0原文

每当我这样做时:

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

xxx 加载,但它的宽度和高度保持为 0。每当我尝试将其加载到 NSImageCell 中时,我都会收到此错误:

NSImageCell 的对象值必须是 NSImage。

有人可以帮我吗?我以前从未遇到过这个问题。

编辑:抱歉,我错过了这一点。因此,当我在数据源委托中执行此操作时,它不起作用,并且在“return cell;'。

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

编辑2:这变得越来越严重。我不明白发生了什么,但图像正确加载了高度和宽度,但当我将其添加到 NSImageCell 时它仍然抱怨。

替代文字

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

默嘫て 2024-09-21 22:55:34

我明白了,所以表视图方面毕竟是相关的。

作为数据源,您的工作是返回(并在用户编辑的情况下接收)列中单元格的对象值。

但你并没有返回这样的值;你正在归还一个手机。因此,您尝试将图像单元格(由数据源创建)设置为图像单元格(该列拥有的现有单元格)的值。

日志消息表明您在创建列时已将列的单元格设置为图像单元格,因此您现在需要做的就是更改数据源以始终返回列的对象值,而不是单元格。对于图像列,返回图像。对于文本列,返回字符串。

请注意,NSTableView 的工作方式与 UITableView 不同,其中 UITableViewCells 是 UIView,并且屏幕上的单元格数量与行数一样多;在 NSTableView 中,每个 NSTableColumn 都获取一个且仅有一个数据单元格,并且该单元格用于绘制每一行的该列。单元格绘制其对象值,您可以通过从数据源方法返回该对象值(对象值)来将其提供给单元格。

有关控件(NSTableView 是 NSControl 的一种)及其单元格的文档是 控制和单元编程指南

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.

命比纸薄 2024-09-21 22:55:34

我的猜测是它返回nil,在这种情况下获取宽度/高度将返回 0。

尝试:

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

并确保图像实际上被复制到应用程序的资源文件夹中!

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!

梦巷 2024-09-21 22:55:34

确定您实际上正在接收宽度和高度为零的 NSImage 实例(不是 nil),下一步是确定发生这种情况的原因。

  1. 您可以在预览中打开 Package.png 吗?
    1. 如果是这样:预览信息窗口中的宽度和高度是多少?它有任何分辨率(DPI 或每米像素)信息吗?如果是这样,那说明了什么?
    2. 如果您破解应用程序包,您可以在预览中打开 Package.png 的副本吗?
  2. 当您记录图像的描述时,输出是什么?
  3. 记录图像的 representations 数组的输出是什么? (这应该包含在图像自己的描述中,但我明确包含它,以防万一。)
  4. 如果删除构建文件夹并重新构建,会发生什么?

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?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文