2015-02-06 63 views
0

我嘗試了下面的代碼來獲取使用objective-c完整的iPhone屏幕的屏幕截圖。它在iPhone中使用objective-c完美地工作。但我沒有獲得swift中全屏的屏幕截圖。任何人都可以給我提供一些信息關於這個?swift有沒有替代UIGetScreenImage的?

CGImageRef UIGetScreenImage(void); 
CGImageRef screen = UIGetScreenImage(); 
UIImage* image = [UIImage imageWithCGImage:screen]; 
CGImageRelease(screen); 

在此先感謝

+0

在swift.Is沒有得到UIGetScreenImage有采取在swift.I全屏截圖任何其他方法嘗試下面的代碼迅速。 var keyWindow:UIWindow = UIApplication.sharedApplication()。keyWindow! VAR RECT:的CGRect = keyWindow.bounds – 2015-02-06 09:19:03

+0

UIGraphicsBeginImageContextWithOptions(rect.size,真,0.0) VAR上下文:CGContextRef = UIGraphicsGetCurrentContext() keyWindow.layer .renderInContext(上下文) VAR capturedScreen:的UIImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext( )讓nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory 讓nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask 如果讓路徑= NSSearchPathForDirectoriesInDomains(nsDocumentDirectory,nsUserDomainMask,真){ – 2015-02-06 09:22:37

+0

如果paths.count> 0 { 如果讓dirPath =路徑[0]爲?字符串{ 讓writePath = dirPath.stringByAppendingPathComponent( 「Image.png」) UIImagePNGRepresentation(capturedScreen).writeToFile(writePath,原子:真) }} } (使用上面的代碼,我只得到視圖屏幕,但不獲取alertview(放置在窗口上方) – 2015-02-06 09:22:53

回答

0

試試這個:

extension UIView { 
    var snapshot: UIImage { 
     get { 
      UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.mainScreen().scale) 
      self.drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true) 
      let image = UIGraphicsGetImageFromCurrentImageContext() 
      UIGraphicsEndImageContext() 
      return image 
     } 
    } 
}