应用程序处于横向模式时拍摄屏幕截图

时间:2012-12-03 11:13:56

标签: ios ipad uiinterfaceorientation

我正在开发一款仅适用于风景的应用。在那个应用程序中,我有一个功能,截取该特定屏幕的截图。我已经实现了这段代码

UIGraphicsBeginImageContext(self.view.window.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = UIGraphicsGetImageFromCurrentImageContext();;
imageView.transform = CGAffineTransformMakeRotation(3.0 * M_PI / 2.0);
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(imageView.image, nil, nil, nil);

使用此代码,我以纵向模式拍摄了我的应用程序的屏幕截图。我想在横向模式下使用它。

2 个答案:

答案 0 :(得分:6)

不要添加window。如果你添加它,你的上下文大小是错误的。

// Just use self.view.bounds.size is OK
UIGraphicsBeginImageContext(self.view.bounds.size);

答案 1 :(得分:0)

也许很晚,但将它包装在UIImageView中实际上并不会旋转图像本身。 这是我写的一些代码,它从应用程序的完整窗口创建一个旋转的图像。

UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];

CGRect rect = [keyWindow bounds];
UIGraphicsBeginImageContext(rect.size);

CGContextRef context = UIGraphicsGetCurrentContext();
[keyWindow.layer renderInContext:context];

UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return [[UIImage alloc] initWithCGImage:[image CGImage] scale:1 orientation:UIImageOrientationLeft];

如果你想让它向右旋转,只需用UIImageOrientationRight替换UIImageOrientationLeft