0

我正在使用一种方法以编程方式截取 UIImage 屏幕截图,效果非常好。我现在想要做的是从屏幕截图中删除最顶部的 UIView不要从屏幕上的实际视图中删除它,所以简单的设置是topView.hidden = YES行不通的。我正在使用这种方法来保存屏幕截图:

- (UIImage*)screenshot
{
CGSize imageSize = [[UIScreen mainScreen] bounds].size;
if (NULL != UIGraphicsBeginImageContextWithOptions)
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 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 *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;
}

我的第一个想法是从超级层中删除子层,这样做:

NSArray *sublayers = [[window layer] sublayers];
[[window layer] replaceSublayer:[sublayers objectAtIndex:[sublayers count] - 1] with:nil];

但我发现sublayers数组只有 1 个对象。如何从屏幕截图中删除顶视图?

4

1 回答 1

0

例如,您的视图层次结构中有 View1 并且您不想截取 View1 的屏幕截图,那么您可以执行 View1.hiden= TRUE; 在屏幕截图之前,您可以执行 View1.hiden= FALSE; 截图后。

可能这会对你有所帮助。

于 2013-03-02T05:44:08.557 回答