UIBezierPath : Incorrect retrieval of view screen

2019-06-06 07:48发布

I'm taking the screenshot following way.

- (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;
}

On UIView I'm drawing paths using UIBezierPath.

The screenshots that I'm getting are incorrect like this

enter image description here

Any ideas why the upper part is cut to blank ? On UIView all drawing is displayed correctly.


UPDATE : This happens when I draw a long path with UIBezierPath when I release the my brush and get the screenshot it gets the screenshot correctly.

3条回答
我命由我不由天
2楼-- · 2019-06-06 08:06
-(IBAction)takeScreenShot:(id)sender
{

    UIGraphicsBeginImageContext(self.view.bounds.size); 

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

    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
}
查看更多
淡お忘
3楼-- · 2019-06-06 08:08

I saw the another thread which you post. According to the apple technical Q&A, you need to adjust the geometry coordinate first. So, before render the layer of window, do following things first.

// -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);
查看更多
祖国的老花朵
4楼-- · 2019-06-06 08:30
- (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

查看更多
登录 后发表回答