UIBezierPath:不正确的视图截图检索

我正在采取以下方式截图。

- (UIImage*)screenshot { UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow]; CGRect rect = [keyWindow bounds]; UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); [keyWindow.layer renderInContext:context]; UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img; } 

在UIView我使用UIBezierPath绘制path。

我得到的屏幕截图是不正确的

在这里输入图像说明

任何想法为什么上半部分是空白? 在UIView所有绘图显示正确。


更新:当我释放我的画笔并获得正确的屏幕截图时,使用UIBezierPath绘制长path时,会发生这种情况。

我看到了你发布的另一个线程。 根据苹果技术问答 ,您需要先调整几何坐标。 所以,在渲染窗口层之前,先做下面的事情。

 // -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); 
 - (UIImage*)screenshot :(UIView*)vw { CGRect rect = [vw bounds]; UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); [vw.layer renderInContext:context]; UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img; } 

使用这一个

 -(IBAction)takeScreenShot:(id)sender { UIGraphicsBeginImageContext(self.view.bounds.size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); }