views:

373

answers:

1

I successfully implemented an application that works on ipad as well as on iphone. In that I provided option to user to send the screen shot of the application as mail attachment. Even that is functioning well. But my code is taking screen shot irrespective of orientation. The image i'm getting is always in portrait mode. I want to take the screen shot depending on the orientation of ipad/iphone and send the image as attachment. I'm using following code to take the screen shot.

UIGraphicsBeginImageContext(screenWindow.frame.size);
[screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
A: 

There may be a top level view you could render besides the actual window that is already rotated. If you are showing the status bar, try getting the superview of that. Otherwise something like this might work:

CGRect frame = screenWindow.frame;

if ( isLandscape ) {
  CGFloat t = frame.size.width;
  frame.size.width = frame.size.height;
  frame.size.height = t;
}

UIGraphicsBeginImageContext(frame.size);

if ( isLandscape ) {
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextRotateCTM( context , M_PI_2 );
  CGContextTranslateCTM( context , ( frame.size.width - frame.size.height ) * 0.5 ,
    ( frame.size.height - frame.size.width ) * 0.5 );
}

...

You will probably have to mess around with CGContextTranslateCTM but it should be close to that.

drawnonward