我正在为 iPhone 应用程序编写图像编辑器。我用GPUImage library在 Canvas 上呈现 Sprite 的图像(在 GPUImageView 上)。我使用以下方法来获取 Canvas 的屏幕截图:
@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
当我使用此方法获取关键窗口的整个屏幕截图后, Canvas 的字段为空(没有任何 Sprite 的图像)。
UIView *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIImage *snapshotIamge = [keyWindow snapshot];
// Present the image on the view...
我认为获取包含 GPUImageView 的 View 的屏幕截图存在问题,该 View 使用 OpenGLES 渲染图像而不是 Quartz。如何获取包含GPUImageView的 View 的屏幕截图?
截屏时是否可以使用特定的 CGRect 获取裁剪后的图像(如 Mac 上的 cmd + shift + 4
)?
最佳答案
正如您所发现的,-renderInContext:
不适用于 OpenGL ES 内容。您需要的是从 GPUImage 本身提取处理后的图像。
为此,请在直接输入 GPUImageView 的过滤器上调用 -imageFromCurrentlyProcessedOutput
。这将为您提供一个 UIImage,其中包含最终的过滤图像。然后,您可以直接使用它,也可以将其与周围 View 中的剩余内容合成。
关于uiview - 如何获取包含GPUImageView的 View 的屏幕截图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19218126/