如何获取包含GPUImageView的视图的屏幕截图?

时间:2013-10-07 06:14:13

标签: uiview uiimage screenshot gpuimage

我正在为iPhone应用程序编写图像编辑器。我使用GPUImage library在画布上呈现精灵的图像(在GPUImageView上)。我使用以下方法获取画布的屏幕截图:

@implementation UIView (SnapshotImage)
// Return a snapshot image of this view
- (UIImage*)snapshot{

    UIGraphicsBeginImageContextWithOptions(self.bounds.size,YES,0.0f);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // In iOS 7, you can use the following instead of `-renderInContext:`. It should be faster.
    // [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
    [self.layer renderInContext:context];

    UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return capturedImage;
}
@end

在我使用此方法获取关键窗口的整个屏幕截图后,画布的字段为空(没有任何精灵的图像)。

UIView *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIImage *snapshotIamge = [keyWindow snapshot];
// Present the image on the view...

我认为获取包含GPUImageView(s)的视图的屏幕截图存在问题,该视图使用 OpenGLES 来呈现不是 Quartz 的图像。如何获取包含GPUImageView的视图的屏幕截图?

在拍摄屏幕截图(如Mac上的cmd + shift + 4)时,是否可以使用特定的CGRect获取裁剪后的图像?

1 个答案:

答案 0 :(得分:2)

如您所见,-renderInContext:不适用于OpenGL ES内容。您需要的是从GPUImage本身提取处理过的图像。

要执行此操作,请在直接反馈到GPUImageView的过滤器上调用-imageFromCurrentlyProcessedOutput。这将为您提供一个UIImage,其中包含最终的过滤图像。然后,您可以直接使用它,也可以将其与周围视图中的剩余内容合成。

相关问题