2017-08-10 5 views
0

ボタンを使ってビューコントローラ全体の写真を撮りたいだけです。私がやっていることは、uiviewのスクリーンショットを撮ってフォトギャラリーに保存することだと思います。私は写真のボタンが欲しい。ビュー全体のスクリーンショットを取る(swift3)

import UIKit 

class ViewController: UIViewController { 

    @IBOutlet var x: UIButton! 
    var screenShot: UIImage? 
override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
       } 

    func saveScreen() { 
     self.screenShot = screenshot() 
     print("cool") 
    } 

    func screenshot() -> UIImage { 
     let imageSize = UIScreen.main.bounds.size as CGSize; 
     UIGraphicsBeginImageContextWithOptions(imageSize, false, 0) 
     let context = UIGraphicsGetCurrentContext() 
     for obj : AnyObject in UIApplication.shared.windows { 
      if let window = obj as? UIWindow { 
       if window.responds(to: #selector(getter: UIWindow.screen)) || window.screen == UIScreen.main { 
        // so we must first apply the layer's geometry to the graphics context 
        context!.saveGState(); 
        // Center the context around the window's anchor point 
        context!.translateBy(x: window.center.x, y: window.center 
         .y); 
        // Apply the window's transform about the anchor point 
        context!.concatenate(window.transform); 
        // Offset by the portion of the bounds left of and above the anchor point 
        context!.translateBy(x: -window.bounds.size.width * window.layer.anchorPoint.x, 
             y: -window.bounds.size.height * window.layer.anchorPoint.y); 

        // Render the layer hierarchy to the current context 
        window.layer.render(in: context!) 

        // Restore the context 
        context!.restoreGState(); 
       } 
      } 
     } 
     let image = UIGraphicsGetImageFromCurrentImageContext(); 
     return image! 
    } 
    @IBAction func saveUIVIEW(_ sender: Any) { 
     self.screenShot = screenshot() 
    } 

} 

enter image description here

答えて

0

このコードを使用します。元の答えはここにあります。 Screenshot on swift programmatically

func screenshot() -> UIImage { 
    let imageSize = UIScreen.main.bounds.size as CGSize; 
    UIGraphicsBeginImageContextWithOptions(imageSize, false, 0) 
    let context = UIGraphicsGetCurrentContext() 
    for obj : AnyObject in UIApplication.shared.windows { 
     if let window = obj as? UIWindow { 
      if window.responds(to: #selector(getter: UIWindow.screen)) || window.screen == UIScreen.main { 
           // so we must first apply the layer's geometry to the graphics context 
           context!.saveGState(); 
           // Center the context around the window's anchor point 
           context!.translateBy(x: window.center.x, y: window.center 
            .y); 
           // Apply the window's transform about the anchor point 
           context!.concatenate(window.transform); 
           // Offset by the portion of the bounds left of and above the anchor point 
       context!.translateBy(x: -window.bounds.size.width * window.layer.anchorPoint.x, 
            y: -window.bounds.size.height * window.layer.anchorPoint.y); 

           // Render the layer hierarchy to the current context 
           window.layer.render(in: context!) 

           // Restore the context 
           context!.restoreGState(); 
      } 
     } 
    } 
    let image = UIGraphicsGetImageFromCurrentImageContext(); 
    return image! 
} 

コール:私は、この関数を呼び出すにはどうすればよい

func saveScreen() { 
    let image = screenshot() 
    UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil) 
} 

func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) { 
    if let error = error { 
     // we got back an error! 
     let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert) 
     ac.addAction(UIAlertAction(title: "OK", style: .default)) 
     present(ac, animated: true) 
    } else { 
     let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .alert) 
     ac.addAction(UIAlertAction(title: "OK", style: .default)) 
     present(ac, animated: true) 
    } 
} 

@IBAction func saveUIVIEW(_ sender: Any) { 
    saveScreen() 
} 
+0

?ボタンにスクリーンショットを入れようとしましたが、うまくいきませんでした。 –

+0

答えを更新しました。 –

+0

私の質問が更新されました。私のコードは実行時エラーはありませんが、写真ギャラリーには何も保存されていません。 –

関連する問題