(我已经阅读了这个页面,但它并没有帮助我CALayer 不显示)
我有一个类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);
}
这是我的窗口控制器:
@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 *)