Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to take a snapshot of a MGLKView ? #95

Open
alfonsotesauro opened this issue Dec 15, 2023 · 1 comment
Open

How to take a snapshot of a MGLKView ? #95

alfonsotesauro opened this issue Dec 15, 2023 · 1 comment

Comments

@alfonsotesauro
Copy link

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:

  • (UIColor *) GetCurrentPixelColorAtPoint:(CGPoint)point
    {
    unsigned char pixel[4] = {0};

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast);

    CGContextTranslateCTM(context, -point.x, -point.y);

    [self.view.layer renderInContext:context];

    CGContextRelease(context);

    CGColorSpaceRelease(colorSpace);

    NSLog(@"pixel: %d %d %d %d", pixel[0], pixel[1], pixel[2], pixel[3]);

    UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0];

    return color;
    }

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:

  • (UIColor *) GetCurrentPixelColorAtPoint:(CGPoint)point
    {
    unsigned char pixel[4] = {0};

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast);

    CGContextTranslateCTM(context, -point.x, -point.y);

    [self.glView.glLayer renderInContext:context];

    CGContextRelease(context);

    CGColorSpaceRelease(colorSpace);

    NSLog(@"pixel: %d %d %d %d", pixel[0], pixel[1], pixel[2], pixel[3]);

    UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0];

    return color;
    }

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
Copy link

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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants