- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以,我的 Cocoa 应用程序中遇到了这个奇怪的问题。一个工具栏项,和一些 NSImageViews 只是渲染他们的图像颠倒,没有明显的原因。当我出于某种原因启动应用程序时,工具栏项目实际上是这样的(图标是在代码中分配的),然后,当我使用取景器缩放效果打开HUD时,其中包含的 ImageView 是颠倒的。
上面的图片是这种奇怪现象的屏幕截图。正如您所看到的,HUD 中的每个 NSImageView 中的“显示垃圾”项旋转得很奇怪,但 NSBrowser 中的 NSImageView 没有问题。
我该如何追踪导致此问题的原因,然后进行修复?
最佳答案
参见Mac OS X Developer Release Note - AppKit Release Notes (Snow Leopard) :
Flipped images in toolbars (New since January 2009 seed)
A common but incorrect practice is to call
setFlipped:YES
on an image that you plan to draw into a flipped graphics context. This is incorrect because if the image is later drawn into a normal unflipped context, the image will appear upside down. However, Leopard and earlier contained a bug in which images set onNSToolbarItem
(viasetImage:
) would ignore theirisFlipped
property. SnowLeopard fixes this bug for apps compiled on SnowLeopard with the 10.6 SDK; as a result, some images which should have drawn upside down in Leopard, will begin doing so when your app is recompiled.If you recompile your app on SnowLeopard and discover that your toolbar item images are drawing upside down, then it indicates that you are calling
setFlipped:YES
somewhere within your code. You should remove those calls and replace the image drawing with methods that correctly handle flipped contexts. See the discussion ofsetFlipped:
in these Release Notes for more discussion.
回复:setFlipped:
NSImage: deprecating -[NSImage setFlipped:], adding a drawing method that respects context flippedness (New since WWDC 2008)
The flipped attribute of
NSImage
is widely misunderstood. We are deprecating it for SnowLeopard, and replacing its typical uses with less error-prone API.The property describes the orientation of the internal coordinate system of the
NSImage
. Just as a superview never cares about the flippedness of its subviews, a user of anNSImage
should not care about its flippedness.The typical (flawed) use case is to try to call
[image
just prior to drawing, but this does not accomplish the intended goal. If called before caching, then representations end up caching upside down, and the flip is absorbed into the cache. If called after caching, it has no effect-the cached representation is already supposed to incorporate any necessary flipping. In the former case, if the
setFlipped:[[NSGraphicsContext
currentContext] isFlipped]]NSImage
is drawn anywhere else later, it ends up upside down in that place, which is also confusing because the bug and the expression of the bug are far apart. Lack of understanding regarding flippedness is also frequently the source of poorly performing code, in which people make unnecessary intermediate buffers to work around perceived framework bugs. The framework behaves according to design, but contrary to expectation, and the semantics are not all that useful. It's also difficult to change the semantics of-[NSImage isFlipped]
, because a lot of code is very closely dependent on the current behavior. Rather than attempt this, we have deprecated the property.We are providing a simple and correct way to draw images in a flipped or unflipped context, which is a draw method that can account for context flippedness. We are also adding a hints parameter matching the hints in
-bestRepresentationForRect:context:hints:
.
- (void)drawInRect:(NSRect)dstRect
fromRect:(NSRect)srcRect
operation:(NSCompositingOperation)op
fraction:(CGFloat)alpha
respectFlipped:(BOOL)respectContextIsFlipped
hints:(NSDictionary *)hints;Pass
YES
forrespectFlipped
to get the fancy new behavior. One note for those that understand the CTM and worry that this method has an odd interaction, where modifying the CTM could fail to have any effect on image drawing: This is not the case. This method branches behavior based on[[NSGraphicsContext currentContext]
. Modifying the CTM might turn your axes upside down, but it will not alter the result of
isFlipped]-[NSGraphicsContext isFlipped]
. They're completely orthogonal.A second valid use of
-[NSImage
was to specify the flippedness of the context obtained via
setFlipped:]-[NSImage lockFocus]
. There are cases, for example drawing directly viaNSLayoutManager
, that require a flipped context. To cover this case, we add
- (void)lockFocusFlipped:(BOOL)flipped;
This doesn't alter the state of the image itself, only the context on which focus is locked. It means that (0,0) is at the top left and positive along the Y-axis is down in the locked context.
关于cocoa - NSImageViews 中的图像随机颠倒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4754973/
我有一个 NSTableView,它包含所有 NSImageView 的名称,并且根据 NSImageView 的添加顺序,最后一个将位于前面。 但是如果我希望用户能够将 NSImageViews 放
我有一个 NSImageView 可以很好地作为图像运行。它也是通过绑定(bind)的。使用值路径属性显示每个记录的已保存图像的值转换器(值转换器只是附加文件路径——我只是将图像的文件名存储在数据库中
我想在 NSImageView 中捕捉图像变化的事件,我该怎么做? 最佳答案 您可以尝试使用 KVO监视image键。如果这不起作用,您可能必须继承 NSImageView 并将逻辑插入到 setIm
我创建了 NSImageView 的子类,因为我想自定义一些 mouseClick 事件。但是,我还没开始,就出了问题。我创建了 NSImageView 子类: (文件/新建/文件.../Cocoa(
所以,我的 Cocoa 应用程序中遇到了这个奇怪的问题。一个工具栏项,和一些 NSImageViews 只是渲染他们的图像颠倒,没有明显的原因。当我出于某种原因启动应用程序时,工具栏项目实际上是这样的
我是Mac开发新手,我们有类似的方法吗 imagev = [NSArray arrayWithObjects 我需要一些类似于我们在 iOS 中所做的事情想要在 mac 中做的事情, imageVie
任何人都可以给我推荐一个在 NSImageView 中缩放图像的好示例代码吗?谢谢。 最佳答案 如果你看the documentation ,你会发现 NSImageView 不支持这一点。使用Ima
我有一个 Cocoa 应用程序。在窗口 View 中我放置了一个 NSImageView。使用自动布局,我添加了前导和尾随空格,使得 NSImageView 锚定到 View (以及窗口)。 NSIm
目前我想创建一个圆角NSImageView,我是新手,怎么做? 最佳答案 我不知道这是否有效,所以请尝试一下,我们会祈祷的。在 iPhone 上,您可以使用任何 UIView(iOS 中的 NSVie
所以我习惯了UIImageView ,并能够设置其图像在其中显示的不同方式。例如 AspectFill模式等... 我想使用 NSImageView 完成同样的事情在 Mac 应用程序上。是否NSIm
我有一个使用以下代码的 iOS 应用程序。我现在正在为 Mac OS X 创建应用程序并希望获得相同的效果,但是我似乎无法使用 clipsToBounds .我应该使用什么? 我有其余的代码在 Mac
我正在玩 AppKit 和 NSDocument,但我不知道为什么这不起作用?: 我刚刚写了这个,图像不是零,但从不加载任何图像,它的大小始终为零。 这是我必须实现的正确方法才能将文件读入我的文档吗?
我的 Mac 应用程序中有一些 NSImageView,用户可以在其中拖放 .png 或 .pdf 等对象,将它们存储到用户共享默认值中,这很好用。 我现在想在用户双击这些 NSImageView 时
我是 OS X 编程的初学者,在设置 NSImageView 透明度时遇到问题。 在 iOS 中这很简单,在 UIImageView 中有一个名为 alpha 的属性,我可以更改它并为其设置动画。 (
我如何能够在 Objective C 中屏蔽 nsimageview?例如,有一个带圆角的 nsimageview。 最佳答案 你不知道。如果你想以 NSImageView 不支持的方式绘制图像,则需
出于某种原因,分配两个 NSImageView 会造成巨大的内存泄漏。 尽管 dealloc 方法在两个 NSImageView 中都会被调用。 我正在使用ARC。 使用NSImageViews 一旦
我正在尝试制作一个简单的双骰子游戏。在过去的一年里,我完成了大量的 C 编程,并且正在尝试转换为 GUI 和 Obj C。我的窗口内有一个按钮和两个图像单元。图像被视为输出(染料、染料2),按钮保持
我有多个不同尺寸和比例的 NSImageView,它们垂直堆叠在水平空间有限的 View 中。 由于图像尺寸可能大于父 View 的尺寸,我想使用 NSImageScaleProportionally
我已经 Hook 了 NSTableView 的选择更改事件。我需要在 ImageView 中显示所选图像 func tableViewSelectionDidChange(_ notificatio
我创建了一个 NSView 的自定义子类来控制 2 个 NSImageView。创建图像的代码如下: - (id)initWithFrame:(NSRect)frameRect AndMap:(NSI
我是一名优秀的程序员,十分优秀!