Objective c iPhone4 iOS在没有导航栏和选项卡栏的情况下拍摄视图截图?

Objective c iPhone4 iOS在没有导航栏和选项卡栏的情况下拍摄视图截图?,objective-c,cocoa-touch,core-graphics,screenshot,Objective C,Cocoa Touch,Core Graphics,Screenshot,我得到这段代码是为了能够截图 UIGraphicsBeginImageContext(scrollView.bounds.size); [scrollView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); NSData * data = UIImagePN

我得到这段代码是为了能够截图

UIGraphicsBeginImageContext(scrollView.bounds.size);
[scrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
但是,即使我将上下文设置为320x480,滚动视图的部分仍然没有显示。scrollview管理的视图可以完全适合320x480框架,但状态栏、导航栏和选项卡栏涵盖了部分视图

我想拍摄视图的全屏(320x480)截图,视图的各个部分被状态栏、选项卡栏和导航栏所覆盖。有关于如何操作的提示吗

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


谢谢大家!

首先制作整个屏幕的屏幕截图:

// 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();

我在这个网站上发现了以下内容:


你可能还想看看这个(苹果的截图示例):

我澄清了这个问题。我想做的是显示视图的部分,由tabbar覆盖,等等。跟踪所有不应该成为屏幕截图一部分的视图。在调用
takeScreenShot
方法之前,在调用屏幕截图之后隐藏所有这些视图。解开它们。我有一个工具栏和一个按钮,用这种方式修复它很容易,也不太费时。不起作用:我的OpenGL视图没有在结果屏幕快照中呈现。这是一个很好的观点,我没有用OpenGL测试它。这是有道理的,那是行不通的。在我的例子中,我通过获取OpenGL像素缓冲区来修复它,并手动将其返回到代码生成的图像的正确位置。但这不是一个通用的解决方案。很酷,您可以通过添加可选的openGL代码来更新我的答案:)
UIGraphicsBeginImageContext(YourView.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);