kakashidinho / metalangle

MetalANGLE: OpenGL ES to Metal API translation layer
Other
462 stars 65 forks source link

How to take a snapshot of a MGLKView ? #95

Open alfonsotesauro opened 11 months ago

alfonsotesauro commented 11 months ago

Hello, thanks to everybody for the attention. I develop a iOS application that uses a SDK that uses the MetalAngle framework. I need to capture the color of pixels on the frontmost view in my app. For standard UIViewControllers, I use simply:

If I try to make the same on my MGLKView I always get a clearColor, regardless of the appearance of the view. I know that point is not transparent, so there must be something weird going on. for MGLKView, I simply do the same:

So the question is, if you don't want to read the code, how to take a UIImage or a UIColor of a pixel of a MGLKView on iOS ?

Thanks to everybody and sorry for the long message.

myonlylonely commented 3 weeks ago

I have replaced Ejecta's original OpenGLES implemention with MetalANGLE. Here is the code that takes a screenshot from MGLKView in Ejecta . Not precisely what you want.

EJCanvasContext *screenCanvasContext = scriptView.screenRenderingContext;
EJCanvasContextWebGLScreen *screenWebGLCanvasContext = (EJCanvasContextWebGLScreen*)screenCanvasContext;

UIGraphicsBeginImageContextWithOptions(CGSizeMake(CGRectGetWidth(screenWebGLCanvasContext.view.frame), CGRectGetHeight(screenWebGLCanvasContext.view.frame)), YES, 0.0);
[screenWebGLCanvasContext.view drawViewHierarchyInRect:screenWebGLCanvasContext.view.bounds afterScreenUpdates:NO];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

if(image) {
    NSData *raw = UIImagePNGRepresentation(image);
}