【问题标题】:iPhone4 iOS take screenshot of the view without NavBar and TabBar?iPhone4 iOS 在没有 NavBar 和 TabBar 的情况下截取视图?
【发布时间】:2011-11-21 22:11:58
【问题描述】:

我得到了这段代码来截取视图。

UIGraphicsBeginImageContext(scrollView.bounds.size);
[scrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);

但是,即使我将上下文设置为 320x480,滚动视图的部分内容仍然不会显示。滚动视图管理的视图可以完美地适应 320x480 框架,但它的一部分被状态栏、导航栏和 TabBar 覆盖。

我想对视图的部分进行全屏 (320x480) 屏幕截图,状态栏、TabBar 和 NavBar 可见。 有关如何执行此操作的任何指示?

一个额外的问题,可能是相关的:生成的图像使用 x1 比例,并且在视网膜显示器上看起来非常模糊,缩放需要更大的图像并将其缩小。这意味着我需要渲染 640x960 的屏幕截图来重现原始的清晰质量。我该怎么做呢?

谢谢!

【问题讨论】:

  • 我澄清了这个问题。我想要做的是显示视图的各个部分,被标签栏等覆盖。
  • 跟踪所有不应出现在屏幕截图中的视图。在调用 takeScreenShot 方法之前,在调用屏幕截图后隐藏所有这些视图。取消隐藏它们。我有一个工具栏和一个按钮,以这种方式修复它很容易,而且耗时更少。

标签: objective-c cocoa-touch core-graphics screenshot


【解决方案1】:

我在这个网站上找到了以下内容:http://www.icodeblog.com/2009/07/27/1188/

UIGraphicsBeginImageContext(YourView.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

您可能还想查看这个(Apple 的如何制作屏幕截图的示例):http://developer.apple.com/library/ios/#qa/qa1703/_index.html

【讨论】:

    【解决方案2】:

    先进行全屏截图:

    // Create a graphics context with the target size
    // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
    // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
    CGSize imageSize = [[UIScreen mainScreen] bounds].size;
    if (NULL != UIGraphicsBeginImageContextWithOptions)
        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
    else
        UIGraphicsBeginImageContext(imageSize);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // Iterate over every window from back to front
    for (UIWindow *window in [[UIApplication sharedApplication] windows]) 
    {
        if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
        {
    
            // -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, [window center].x, [window center].y);
            // Apply the window's transform about the anchor point
            CGContextConcatCTM(context, [window transform]);
            // Offset by the portion of the bounds left of and above the anchor point
            CGContextTranslateCTM(context,
                                  -[window bounds].size.width * [[window layer] anchorPoint].x,
                                  -[window bounds].size.height * [[window layer] anchorPoint].y);
    
            // Render the layer hierarchy to the current context
            [[window layer] renderInContext:context];
    
            // Restore the context
            CGContextRestoreGState(context);
        }
    }
    
    // Retrieve the screenshot image
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    

    然后裁剪到合适的大小

    CGImageRef subImageRef = CGImageCreateWithImageInRect(screenshot.CGImage, rect);
    CGRect smallBounds = CGRectMake(0, 64, 320, 372); //You should remove the hard coded numbers
    
    UIGraphicsBeginImageContext(smallBounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, smallBounds, subImageRef);
    UIImage* cropped = [UIImage imageWithCGImage:subImageRef];
    UIGraphicsEndImageContext();
    

    【讨论】:

    • 不起作用:我的 OpenGL 视图未在生成的屏幕截图中呈现
    • 这很好,我没有用 OpenGL 测试它。这样做是有道理的。
    • 是的。就我而言,我通过获取 OpenGL 像素缓冲区来修复它,并在正确的位置手动将其复制到代码生成的图像中。不过,这不是一个通用的解决方案。
    • 酷,您可以通过添加可选的 openGL 代码来更新我的答案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    相关资源
    最近更新 更多