(我已閱讀此頁,但它並沒有幫助我CALayer not displaying)的CALayer不顯示
我有一個叫Image
類具有此場數據:
uint8_t *data;
我已經使用這data
對一個CALayer
,我得到了從互聯網上收集代碼顯示該Image
。
我看到如何在我的應用程序創建另一個窗戶,我把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);
}
這是我窗口的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 *)
顯示的代碼也懶得解釋變量是一回事。將圖層添加到圖層是另一回事。 –
對不起,我沒有得到它。你是說我不應該這樣做? [display.layer addSublayer:histogramDisplayLayer]; –