我有一个 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 thelayerContentsForContentsScale:
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 thekCAGravityResize
,kCAGravityResizeAspect
, andkCAGravityResizeAspectFill
modes.
关于macos - 为什么在 NSView 中使用大多数内容重力常量时会发生崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22311705/