在OSX中通过视频预览添加addSublayer

时间:2015-07-02 12:52:24

标签: macos cocoa

我试图在视频预览中添加简单的文字标签。视频预览工作正常,但文本标签无处可寻。编译器也没有给我任何错误或警告。我知道我在那里混合点符号,但我不确定这是否是我的根本原因。

这是我的.m文件中的剪辑:

@interface AVRecorderDocument ()
    @property (retain) AVCaptureVideoPreviewLayer *previewLayer;
    @property (nonatomic, strong) NSTextField *labelVideoOverlay;
@end

- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
    CALayer *previewViewLayer = [[self previewView] layer];

    self.labelVideoOverlay = [[NSTextField alloc] initWithFrame:CGRectMake(100, 120, 200, 40)];
    self.labelVideoOverlay.stringValue = @"TEST";
    self.labelVideoOverlay.backgroundColor = [NSColor blackColor];
    self.labelVideoOverlay.textColor = [NSColor whiteColor ];

    [previewViewLayer setBackgroundColor:CGColorGetConstantColor(kCGColorBlack)];
    AVCaptureVideoPreviewLayer *newPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:[self session]];
    [newPreviewLayer setFrame:[previewViewLayer bounds]];

    [newPreviewLayer setAutoresizingMask:kCALayerWidthSizable | kCALayerHeightSizable];

    [previewViewLayer addSublayer: self.labelVideoOverlay.layer];
    [previewViewLayer addSublayer:newPreviewLayer];

    [self setPreviewLayer:newPreviewLayer];
    [newPreviewLayer release];
}

1 个答案:

答案 0 :(得分:0)

虽然您已将视频预览添加为子图层,但仍需要将叠加层作为子视图添加到主视图中。我最近做了类似的事情(仅用UIImageView),但校长应该是相同的。

self.overlayImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"overlaygraphic.png"]];
self.overlayImageView.translatesAutoresizingMaskIntoConstraints = NO;
[[self view] addSubview:self.overlayImageView];

所以,你可以这样做:

self.labelVideoOverlay = [[NSTextField alloc] initWithFrame:CGRectMake(100, 120, 200, 40)];
self.labelVideoOverlay.stringValue = @"TEST";
self.labelVideoOverlay.backgroundColor = [NSColor blackColor];
self.labelVideoOverlay.textColor = [NSColor whiteColor ];
[[self view] addSubview:self.labelVideoOverlay];