1

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.

deimus
  • 9,565
  • 12
  • 63
  • 107
  • Follow this link : http://stackoverflow.com/questions/7964153/ios-whats-the-fastest-most-performant-way-to-make-a-screenshot-programaticall – Pierre Jul 05 '12 at 08:03
  • I've tried also that way, still the image is not correct. I guess the problem is how to UIBezierPath maps its data to layer – deimus Jul 05 '12 at 08:27
  • refer this:http://stackoverflow.com/questions/4475397/convert-uiwebview-contents-to-a-uiimage-when-the-webview-is-larger-than-the-scre – Paresh Navadiya Jul 05 '12 at 08:57
  • its the same, guys do you have any alternative of using CoreGraphics ? – deimus Jul 05 '12 at 09:22

3 Answers3

1

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);
AechoLiu
  • 17,522
  • 9
  • 100
  • 118
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

Prabhat Kasera
  • 1,129
  • 11
  • 28
  • 1
    Basically your solution is not different from mine. The same issue exists also for taking this way. – deimus Jul 05 '12 at 08:24
0
-(IBAction)takeScreenShot:(id)sender
{

    UIGraphicsBeginImageContext(self.view.bounds.size); 

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

    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
}
Anil Kothari
  • 7,653
  • 4
  • 20
  • 25