问题描述:

An item that I am struggling with is when I attempt to take a screen capture of any portion of the GMSMapView (Google Maps SDK iOS). The UIGraphicsGetImageFromCurrentImageContext() UIImage output is invisible. I eventually discovered that the GMSMapView is rendered with OpenGL. Knowing this, I attempted to use the glReadPixels() method. The troubling thing is that I am able to retrieve a map screen capture on the iPhone simulator just fine, but when I use the same method on a real device it is still invisible (iOS 6).

I'm wondering if anyone has run into the same issue? I've done my share of investigating. I could not find anything relevant to my situation. Most of the generic solutions I did find said that I should update a few of the CAEAGLLayer attributes. I assumed the GMSMapView would limit me from finding the appropriate layer, but I was able to find and update the CAEAGLLayer, as seen below:

[(CAEAGLLayer *)[[[mapView_ subviews] objectAtIndex:0] layer] setOpaque: NO];

[(CAEAGLLayer *)[[[mapView_ subviews] objectAtIndex:0] layer] setDrawableProperties:[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES],kEAGLDrawablePropertyRetainedBacking,kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,nil]];

Forums recommended that by setting the layer's OPAQUE attribute to NO and the retained backing property to YES it would alleviate the iOS 6 issue. It did fix it on the simulator, but not the device.

The last fix I stumbled across said to update the main OpenGL view's preserveBackBuffer to YES. This is where my technical expertise ended. I have not been able to find where or how to update the GMSMapView's preserveBackBuffer value. I'm assuming this is the fix that will remedy all of my screen capture issues. Seems to be solely an issue with devices running iOS 6+.

Does anyone have a tested workaround for this issue?

网友答案:

In Google Maps for iOS v1.1, there is GMSScreenshot class which can achieve this.

    UIImage* image = [GMSScreenshot screenshotOfMainScreen];

This has been removed in v1.2, though, as the map layer can now be captured using the standard graphics context methods. Assuming that GMSMapView is a subview of 'self':

- (UIImage *)captureScreen {
    UIGraphicsBeginImageContextWithOptions(self.frame.size, YES, 0.0f);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}
网友答案:

I opened a issue on Google map group for screen capture of GMSMapView. and they said

I believe with the 1.1.0 release this is now possible using GMSScreenshot screenshotOfMainScreen.

相关阅读:
Top