1

我正在按照以下方式截屏。

- (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当我释放我的画笔并获取屏幕截图时,它会正确获取屏幕截图。

4

3 回答 3

1

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

// -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);
于 2012-07-05T10:27:50.770 回答
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;
}

Use This one

于 2012-07-05T08:17:14.380 回答
0
-(IBAction)takeScreenShot:(id)sender
{

    UIGraphicsBeginImageContext(self.view.bounds.size); 

    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 

    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
}
于 2012-07-05T08:21:24.287 回答