通过使用 renderInContext 在 UIImage 上绘制 UIView 来截取屏幕截图时,我的 UIImage 不显示

发布于 2024-11-27 20:38:22 字数 2994 浏览 2 评论 0原文

我正在使用 http://developer.apple 中的示例代码。 com/library/ios/#qa/qa1714/_index.html 几乎逐字,我可以保存我的背景图像或我的叠加层,但我无法将两者结合起来。当我尝试使用以下代码组合它们时,叠加层将背景渲染为白色,并且我假设它覆盖了背景。知道这是为什么吗?

这是我的方法,尝试将视图叠加与图像结合起来:

-(void) annotateStillImage
{
    UIImage *image = stillImage;

    // Create a graphics context with the target size
    CGSize imageSize = [[UIScreen mainScreen] bounds].size;
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);

    CGContextRef context = UIGraphicsGetCurrentContext();


    // Draw the image returned by the camera sample buffer into the context. 
    // Draw it into the same sized rectangle as the view that is displayed on the screen.
    UIGraphicsPushContext(context);

    [image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
    UIGraphicsPopContext();

    // -renderInContext: renders in the coordinate space of the layer,
    // so we must first apply the layer's geometry to the graphics context
    CGContextSaveGState(context);
    // Center the context around the window's anchor point
    CGContextTranslateCTM(context, [[self view] center].x, [[self view] center].y);
    // Apply the window's transform about the anchor point
    CGContextConcatCTM(context, [[self view] transform]);
    // Offset by the portion of the bounds left of and above the anchor point
    CGContextTranslateCTM(context,
                          -[[self view] bounds].size.width * [[[self view] layer] anchorPoint].x,
                          -[[self view] bounds].size.height * [[[self view] layer] anchorPoint].y);

    // Render the layer hierarchy to the current context
    [[[self view] layer] renderInContext:context];

    // Restore the context
    CGContextRestoreGState(context);

    // Retrieve the screenshot image containing both the camera content and the overlay view
    stillImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
}

这是我的方法的一个片段,它创建相关的视图:

CGRect layerRect = [[[self view] layer] bounds];
[[self previewLayer] setBounds:layerRect];
[[self previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),  CGRectGetMidY(layerRect))];
[[[self view] layer] addSublayer:[self previewLayer]];

UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButton setImage:[UIImage imageNamed:@"acceptButton.png"] forState:UIControlStateNormal];
[overlayButton setFrame:[self rectForAcceptButton] ];
[overlayButton setAutoresizesSubviews:TRUE];
[overlayButton setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin];
[overlayButton addTarget:self action:@selector(acceptButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:overlayButton];

我期望注释方法中的图像作为背景,并且接受按钮要添加到顶部的视图图。我得到的只是接受按钮和白色背景,但如果我不运行 renderInContext 方法,我得到的只是图像背景,没有覆盖。

谢谢!

I'm using the sample code from http://developer.apple.com/library/ios/#qa/qa1714/_index.html almost verbatim and I can save my background image or my overlay but I can't combine the two. When I try and combine them using the following code the overlay renders the background white and I'm assuming its overwriting the background. Any idea why this is?

Here is my method which tries to combine the view overlay with the image:

-(void) annotateStillImage
{
    UIImage *image = stillImage;

    // Create a graphics context with the target size
    CGSize imageSize = [[UIScreen mainScreen] bounds].size;
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);

    CGContextRef context = UIGraphicsGetCurrentContext();


    // Draw the image returned by the camera sample buffer into the context. 
    // Draw it into the same sized rectangle as the view that is displayed on the screen.
    UIGraphicsPushContext(context);

    [image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
    UIGraphicsPopContext();

    // -renderInContext: renders in the coordinate space of the layer,
    // so we must first apply the layer's geometry to the graphics context
    CGContextSaveGState(context);
    // Center the context around the window's anchor point
    CGContextTranslateCTM(context, [[self view] center].x, [[self view] center].y);
    // Apply the window's transform about the anchor point
    CGContextConcatCTM(context, [[self view] transform]);
    // Offset by the portion of the bounds left of and above the anchor point
    CGContextTranslateCTM(context,
                          -[[self view] bounds].size.width * [[[self view] layer] anchorPoint].x,
                          -[[self view] bounds].size.height * [[[self view] layer] anchorPoint].y);

    // Render the layer hierarchy to the current context
    [[[self view] layer] renderInContext:context];

    // Restore the context
    CGContextRestoreGState(context);

    // Retrieve the screenshot image containing both the camera content and the overlay view
    stillImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
}

Here is a snippet of my method which creates the view in case that's relevant:

CGRect layerRect = [[[self view] layer] bounds];
[[self previewLayer] setBounds:layerRect];
[[self previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),  CGRectGetMidY(layerRect))];
[[[self view] layer] addSublayer:[self previewLayer]];

UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButton setImage:[UIImage imageNamed:@"acceptButton.png"] forState:UIControlStateNormal];
[overlayButton setFrame:[self rectForAcceptButton] ];
[overlayButton setAutoresizesSubviews:TRUE];
[overlayButton setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin];
[overlayButton addTarget:self action:@selector(acceptButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:overlayButton];

I'm expecting the image in the annotate method to be the background and the accept button from the view drawing to be added on top. All I get is the accept button and a white background but if I don't run the renderInContext method all I get is the image background and no overlay.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老娘不死你永远是小三 2024-12-04 20:38:22

结果我将叠加层添加到与预览图层相同的视图中,而不是将其放在自己的视图上。我通过回顾这个伟大的项目意识到了这一点,该项目涵盖了所有可能的屏幕截图类型: https://github.com/cocoacoderorg/Screenshots

修复方法是在 IB 中创建一个具有透明背景的单独覆盖视图,并将我的覆盖元素添加到其中。

Turns out I was adding the overlay to the same view as the preview layer rather than having it on its own view. I realized this by reviewing this great project which covers all types of screenshots possible: https://github.com/cocoacoderorg/Screenshots

Fix was to create a separate overlay view in IB with a transparent background and add my overlay elements to this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文