Objective c 试图从按钮xcode保存屏幕截图

Objective c 试图从按钮xcode保存屏幕截图,objective-c,ios5,xcode4.3,Objective C,Ios5,Xcode4.3,我试图用一个按钮而不是home(主页)按钮来保存我的应用程序的屏幕。使用self时,我收到一个代码错误 在类型为“…我的视图控制器”的对象上找不到属性窗口 (图像视图在.h中声明为出口) 我知道我这里缺少一些基本的东西,但我想不出来,任何帮助都收到了,我很肯定。它的self.view.window 窗口是UIView上的一个属性 窗口接收器的窗口对象,如果没有,则为nil。 (只读) @属性(非原子,只读)UIWindow*此讨论窗口 如果视图尚未添加到窗口,则属性为nil 在iOS 2.0及

我试图用一个按钮而不是home(主页)按钮来保存我的应用程序的屏幕。使用
self
时,我收到一个代码错误

在类型为“…我的视图控制器”的对象上找不到属性窗口

(图像视图在.h中声明为出口)


我知道我这里缺少一些基本的东西,但我想不出来,任何帮助都收到了,我很肯定。

它的
self.view.window

窗口是UIView上的一个属性

窗口接收器的窗口对象,如果没有,则为nil。 (只读)

@属性(非原子,只读)UIWindow*此讨论窗口 如果视图尚未添加到窗口,则属性为nil

在iOS 2.0及更高版本中可用。在UIView.h中声明

UIViewController没有窗口属性。但是它有一个视图属性,该视图属性有一个窗口属性:)

- (IBAction)saveto:(id)sender {
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale);
else
    UIGraphicsBeginImageContext(self.window.bounds.size);
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
[data writeToFile:@"my.png" atomically:YES];
- (IBAction)saveto:(id)sender {
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
        UIGraphicsBeginImageContextWithOptions(self.view.window.bounds.size, NO, [UIScreen mainScreen].scale);
    else
        UIGraphicsBeginImageContext(self.window.bounds.size);
    [self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData * data = UIImagePNGRepresentation(image);
    [data writeToFile:@"my.png" atomically:YES];
}