2012-06-11 50 views
1

我試圖保存一個屏幕從我的應用程序與使用按鈕,而不是主頁按鈕/關閉事務。林使用self代碼錯誤。試圖保存屏幕截圖形成一個按鈕xcode

我的視圖控制器」 ...「屬性窗口不是類型的對象中找到」

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

(圖像視圖被宣佈爲.H出口)

我知道我在這裏失去了一些東西基本但不能弄清楚,任何幫助greatfully收到

回答

2

self.view.window我敢肯定。:)

- (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]; 
} 

窗口是UIView上的一個屬性。

http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html

窗口接收器的窗口對象,如果其沒有,零。

@property(nonatomic,只讀)UIWindow * window討論如果視圖尚未添加到窗口,則此屬性爲nil。

供貨情況Available in iOS 2.0及更高。聲明在UIView.h

UIViewController沒有窗口屬性。但是它有它確實有一個窗口屬性:)

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIViewController_Class/Reference/Reference.html

+0

謝謝視圖屬性,這就是固定的錯誤 – JSA986