目标:获取带有叠加层(MKOverlayView)以及注释的地图屏幕截图。
我做了什么 :
我有一个地图视图、叠加视图以及一个注释视图。这一切都由一个控制器处理。
我正在使用 Apple 提供的代码截取屏幕截图
// 创建一个具有目标大小的图形上下文
// On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
// On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
CGSize imageSize = [[UIScreen mainScreen] bounds].size;
if (NULL != UIGraphicsBeginImageContextWithOptions)
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
else
UIGraphicsBeginImageContext(imageSize);
CGContextRef context = UIGraphicsGetCurrentContext();
// Iterate over every window from back to front
for (UIWindow *window in [[UIApplication sharedApplication] windows])
{
if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
{
// -renderInContext: renders in the coordinate space of the layer,
// so we must first apply the layer's geometry to the graphics context
CGContextSaveGState(context);
// Center the context around the window's anchor point
CGContextTranslateCTM(context, [window center].x, [window center].y);
// Apply the window's transform about the anchor point
CGContextConcatCTM(context, [window transform]);
// Offset by the portion of the bounds left of and above the anchor point
CGContextTranslateCTM(context,
-[window bounds].size.width * [[window layer] anchorPoint].x,
-[window bounds].size.height * [[window layer] anchorPoint].y);
// Render the layer hierarchy to the current context
[[window layer] renderInContext:context];
//for(self.mapView.overlayView.layer.)
//[self.mapView.overlayView.layer renderInContext:context];
// Restore the context
CGContextRestoreGState(context);
}
}
// Retrieve the screenshot image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
问题是上面这行代码只获取了地图视图和注释视图的截图。覆盖视图不存在。
另外,为了澄清我的叠加视图(MKOverlayView)是一个图像。这不会显示在屏幕截图中。我没有使用过OpenGL。我可以看到作为默认图钉的注释视图,但屏幕截图无法捕获图块。我已经为此工作了很长时间。任何帮助将非常感激!谢谢!
这是我的控制器的样子
->> MY控制器
->> MKMapViewOBject
->> MKMapViewOverlayView
->> MKMapOverlay
->> MKAnnotation
->> MKAnnotationView
那么我的 renderInContext:context 有问题吗?
更多信息 :
在我的叠加视图中绘制瓷砖的以下代码
UIGraphicsPushContext(context);
NSData* data = //get data
if(data)
{
UIImage* img = [[UIImage alloc] initWithData:data];
[img drawInRect:drawRect blendMode:kCGBlendModeNormal alpha:0.4 ];
[img release];
}
UIGraphicsPopContext();