我正在 try catch 屏幕部分以在社交媒体上发布图像。
我正在使用以下代码来捕获屏幕。
- (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
以上代码非常适合捕获屏幕。
问题:
我的 UIView
包含带有过滤图像的 GPUImageView
。当我尝试使用上面的代码捕获屏幕时,GPUImageView 的特定部分不包含过滤后的图像。
我将 GPUImageSwirlFilter
与静态图像(无相机)一起使用。我也试过了
UIImage *outImage = [swirlFilter imageFromCurrentFramebuffer]
但它没有给出图像。
注意: 以下是工作代码,它提供了完美的漩涡效果输出,但我想要在 UIImage
对象中使用相同的图像。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
GPUImageSwirlFilter *swirlFilter = [GPUImageSwirlFilter alloc] init];
swirlLevel = 4;
[swirlFilter setAngle:(float)swirlLevel/10];
UIImage *inputImage = [UIImage imageNamed:gi.wordImage];
GPUImagePicture *swirlSourcePicture = [[GPUImagePicture alloc] initWithImage:inputImage];
inputImage = nil;
[swirlSourcePicture addTarget:swirlFilter];
dispatch_async(dispatch_get_main_queue(), ^{
[swirlFilter addTarget:imgSwirl];
[swirlSourcePicture processImage];
// This works perfect and I have filtered image in my imgSwirl. But I want
// filtered image in UIImage to use at different place like posting
// on social media
sharingImage = [swirlFilter imageFromCurrentFramebuffer]; // This also
// returns nothing.
});
});
1) 我对 GPUImage 的 imageFromCurrentFramebuffer
做错了吗?
2) 为什么 screen capture
代码 not
在输出图像中包含 GPUImageView
部分?
3) 如何在 UIImage
中获取 filtered
图像?
最佳答案
首先,-renderInContext:
不适用于 GPUImageView,因为 GPUImageView 使用 OpenGL ES 进行渲染。 -renderinContext:
不从 CAEAGLLayers 捕获,CAEAGLLayers 用于支持呈现 OpenGL ES 内容的 View 。
其次,您可能在后面的代码中得到了一个 nil 图像,因为您忘记在触发 -processImage
之前在过滤器上设置 -useNextFrameForImageCapture
。否则,您的过滤器将不会在其后备帧缓冲区上停留足够长的时间来从中捕获图像。这是由于 a recent change in the way that framebuffers are handled in memory (尽管这种变化似乎没有得到很好的传达)。
关于ios - 屏幕截图中缺少 GPUImage 输出图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23323505/