iphone/ipad application - taking screen shot

回眸只為那壹抹淺笑 提交于 2019-12-11 13:25:16

问题


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

回答1:


There is a category implemented by Hardy Macia at:

http://www.catamount.com/forums/viewtopic.php?f=21&t=967

It has a bunch of categories on UIImage. The one you want to be using is:

UIimage* newImage = [imageToBeRotated imageRotatedByDegrees:90.0];

Your angle will depend on the current orientation of your device. Use:

[[UIApplication sharedApplication] statusBarOrientation]

to get the orientation of your device.

Hope this helps.




回答2:


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.



来源:https://stackoverflow.com/questions/2831576/iphone-ipad-application-taking-screen-shot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!