How to take a snapshot of a MGLKView ?
alfonsotesauro opened this issue · 0 comments
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.