我遇到了一种情况,我希望我的代码截取屏幕截图,但只截取屏幕的中间部分。
@IBAction func savePhoto(_ sender: UIButton) {
// Does not go into program
let size = CGSize(width: view.frame.size.width, height: view.frame.size.height)
UIGraphicsBeginImageContextWithOptions(CGSize(width:1024.0,height: 1024.0), false, UIScreen.main.scale)
view.layer.render(in: UIGraphicsGetCurrentContext()!)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIImageWriteToSavedPhotosAlbum(newImage!, nil, nil, nil)
}
我确实尝试将其设置为 1024x1024 的屏幕截图,我认为这会起作用,但事实并非如此。
Best Answer-推荐答案 strong>
试试这个,它对我有用。
希望,有帮助。
@IBAction func savePhoto(_ sender: UIButton) {
UIGraphicsBeginImageContextWithOptions(CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height - 100), false, 0.0)
self.view.drawHierarchy(in: CGRect(x: 0, y: 0, width: 1024.0, height: 1024.0), afterScreenUpdates: true)
if let image = UIGraphicsGetImageFromCurrentImageContext() {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
UIGraphicsEndImageContext();
}
关于ios - 以编程方式截取图像,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/49865264/
|