CALayer不显示

时间:2013-11-09 18:43:51

标签: objective-c xcode macos calayer nsview

(我已经阅读了这个页面,但它没有帮助我CALayer not displaying

我有一个名为Image的班级,其中包含以下字段数据:

uint8_t *data;

我已使用此data在我从互联网收集代码的Image上显示此CALayer

我看到了如何在我的应用程序中创建另一个窗口,并在其中放置NSView以使用该方法显示Image,我打算显示后面的直方图,但现在我是只是想再次显示相同的图像:

-(void)wrapImageToCALayer: (CALayer*) layer{
    if(!layer) return;

    CGColorSpaceRef grayColorSpace = CGColorSpaceCreateDeviceGray();

    CGContextRef context = CGBitmapContextCreate(data, width, height, 8, step, grayColorSpace, kCGImageAlphaNone);

    CGImageRef dstImage = CGBitmapContextCreateImage(context);


    dispatch_sync(dispatch_get_main_queue(), ^{
        layer.contents = (__bridge id)dstImage;
    });

    CGImageRelease(dstImage);
    CGContextRelease(context);
    CGColorSpaceRelease(grayColorSpace);
}

这是我的Window Controler:

@implementation HistogramControllerWindowController

@synthesize display;

- (id)initWithWindow:(NSWindow *)window{
    self = [super initWithWindow:window];
    if (self) {
        // Initialization code here.

    }
    return self;
}

- (void)windowDidLoad
{
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.

    [super windowDidLoad];


    histogramDisplayLayer = [CALayer layer];
    [histogramDisplayLayer setContentsGravity:AVLayerVideoGravityResizeAspectFill];
    histogramDisplayLayer.position = CGPointMake(display.frame.size.width/2., display.frame.size.height/2.);
    [histogramDisplayLayer setNeedsDisplay];
    [display.layer addSublayer: histogramDisplayLayer];



}

@end

我这样称呼:

[frame wrapImageToCALayer:histogramDisplayLayer];

请注意,histogramDisplayLayer是外部(CALayer *)

2 个答案:

答案 0 :(得分:2)

您的histogramDisplayLayer没有定义大小,您只需设置位置而不是其大小。所以初始化它的框架,这应该可以解决你的问题。

答案 1 :(得分:1)

只需插入:

[display setWantsLayer:YES]; // view's backing store is using a Core Animation Layer

在:

[display.layer addSublayer: histogramDisplayLayer];

[Best way to change the background color for an NSView]