【发布时间】:2018-01-19 06:10:18
【问题描述】:
我只想使用一个按钮来拍摄整个视图控制器的照片。我想我想做的是截取uiview的屏幕截图并将其保存在照片库中。我想要图片中的按钮。
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()
}
}
【问题讨论】:
标签: ios swift3 uiview save photo