我只想用一个按钮拍摄整个视图控制器的照片。我想我要做的是拍摄一张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()
}
}

发布于 2017-08-10 22:17:17
用这个代码。原来的答案在这里。Screenshot on swift programmatically
UIGraphicsBeginImageContextWithOptions(imageSize,-> UIImage ()->UIImage{ let imageSize = UIScreen.main.bounds.size as CGSize;UIScreen.main.bounds.size false,0) let context = UIGraphicsGetCurrentContext() for obj : AnyObject in UIApplication.shared.windows { if imageSize= obj as?UIWindow { if window.responds( to:#selector(getter: UIWindow.screen)) { //所以我们必须首先将图层的几何结构应用于图形上下文!.saveGState();//将上下文围绕在窗口的锚点上下文!.translateBy(x:window.cent.x,y: window.center .y);//应用窗口对锚点上下文的转换!.concatenate(window.transform);.translateBy(x:-window.bounds.size.width * window.layer.anchorPoint.x,y:-window.bounds.size.height * window.layer.anchorPoint.y);//将图层层次结构呈现给当前上下文window.layer.render(在: context!) //还原上下文!.restoreGState();}} let = UIGraphicsGetImageFromCurrentImageContext();返回图像!}
呼叫:
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()
}https://stackoverflow.com/questions/45623100
复制相似问题