gpt4 book ai didi

macos - 为什么在 NSView 中使用大多数内容重力常量时会发生崩溃?

转载 作者:行者123 更新时间:2023-12-03 16:13:28 24 4
gpt4 key购买 nike

我有一个 NSView 子类,并在 initWithFrame 方法中设置以下内容:

self.wantsLayer = YES;
self.layer.contents = [NSImage imageNamed:@"sprite-sheet"];
self.layer.contentsGravity = kCAGravityLeft;

当我运行它时,我得到了预期的视觉结果......并且崩溃并显示以下日志:

** Misuse of NSImage and CALayer. contentsGravity is left. It should be one of resize, resizeAspect, or resizeAspectFill.

为什么?文档中没有任何迹象表明不应使用其他值。

最佳答案

实际上,您所看到的行为在文档中。它只是在 NSImage 文档中,而不是 CALayer 文档中(我强调):

Using Images with CALayer Objects

Although you can assign an NSImage object directly to the contents property of a CALayer object, doing so may not always yield the best results. Instead of using your image object, you can use the layerContentsForContentsScale: method to obtain an object that you can use for your layer’s contents. That method creates an image that is suited for use as the contents of a layer and that is supports all of the layer’s gravity modes. By contrast, the NSImage class supports only the kCAGravityResize, kCAGravityResizeAspect, and kCAGravityResizeAspectFill modes.

关于macos - 为什么在 NSView 中使用大多数内容重力常量时会发生崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22311705/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com