我正在按照下面的方式截取屏幕截图。
- (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
绘制路径。
我得到的屏幕截图不正确
为什么上半部分被切成空白的任何想法?在UIView上,所有绘图都正确显示。
更新:当我使用UIBezierPath
绘制一条长路径时,当我释放我的画笔并获取截图时,它会正确获取截图。
答案 0 :(得分:1)
我看到了你发布的另一个帖子。 根据{{3}},您需要先调整几何坐标。 因此,在渲染窗口层之前,请先执行以下操作。
// -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);
答案 1 :(得分:0)
- (UIImage*)screenshot :(UIView*)vw
{
CGRect rect = [vw bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[vw.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
使用此
答案 2 :(得分:0)
-(IBAction)takeScreenShot:(id)sender
{
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
}