From 4af27175788242128b552b237f06f9b335e8f9b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Renaudeau?= Date: Fri, 18 Sep 2015 17:25:45 +0200 Subject: [PATCH] simplify code of genPixelsWithView --- RNGL/GLTexture.m | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/RNGL/GLTexture.m b/RNGL/GLTexture.m index 8393520..5dd2e82 100644 --- a/RNGL/GLTexture.m +++ b/RNGL/GLTexture.m @@ -2,6 +2,9 @@ #import "RCTLog.h" #import "RCTUtils.h" +// FIXME: the current approach of using a byte array is probably a bottleneck +// this should be investigated: https://github.com/ProjectSeptemberInc/gl-react-native/issues/6 + GLImageData* genPixelsEmpty (int width, int height) { GLubyte* data = (GLubyte *) malloc(width*height*4*sizeof(GLubyte)); @@ -54,17 +57,11 @@ GLImageData* genPixelsWithImage (UIImage *image) GLImageData* genPixelsWithView (UIView *view) { - float width = RCTScreenScale() * view.bounds.size.width; - float height = RCTScreenScale() * view.bounds.size.height; - GLubyte *data = (GLubyte *)malloc(4 * width * height); - CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB(); - CGContextRef ctx = CGBitmapContextCreate(data, width, height, 8, 4 * width, colourSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); - CGColorSpaceRelease(colourSpace); - CGContextClearRect(ctx, CGRectMake(0.0, 0.0, width, height)); - CGContextScaleCTM(ctx, RCTScreenScale(), RCTScreenScale()); - [view.layer renderInContext:ctx]; - CGContextRelease(ctx); - return [[GLImageData alloc] initWithData:data withWidth:width withHeight:height]; + UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, RCTScreenScale()); + [view drawViewHierarchyInRect:view.frame afterScreenUpdates:YES]; + UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + return genPixelsWithImage(snapshot); } @implementation GLTexture -- 2.26.2