在 Swift 3 中将更改后的图像保存到相机胶卷
Save altered images to camera roll in Swift 3
以下函数将我的背景图像保存到相机胶卷中。最终,这是我的自定义相机刚刚拍摄的图像。
UIImageWriteToSavedPhotosAlbum(self.backgroundImage!, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)
但是如何保存已更改的图像?例如,我在上面放了一个文本框。我应该使用哪个变量而不是 backgroundImage?或者我应该使用不同的功能,如 UIGraphicsImageRenderer?
非常感谢您的帮助。
要从当前视图获取图像,您可以使用例如 CoreGraphics
。下面的代码片段假定在视图控制器中实现并呈现它的整个视图:
UIGraphicsBeginImageContext(self.view.frame.size)
if let ctx = UIGraphicsGetCurrentContext() {
self.view.layer.render(in: ctx!)
let renderedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext() //remember to end context to not cause memory leak
}
其中 renderedImage
是带有文本的图像,前提是渲染时文本字段在视图中。
以下函数将我的背景图像保存到相机胶卷中。最终,这是我的自定义相机刚刚拍摄的图像。
UIImageWriteToSavedPhotosAlbum(self.backgroundImage!, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)
但是如何保存已更改的图像?例如,我在上面放了一个文本框。我应该使用哪个变量而不是 backgroundImage?或者我应该使用不同的功能,如 UIGraphicsImageRenderer?
非常感谢您的帮助。
要从当前视图获取图像,您可以使用例如 CoreGraphics
。下面的代码片段假定在视图控制器中实现并呈现它的整个视图:
UIGraphicsBeginImageContext(self.view.frame.size)
if let ctx = UIGraphicsGetCurrentContext() {
self.view.layer.render(in: ctx!)
let renderedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext() //remember to end context to not cause memory leak
}
其中 renderedImage
是带有文本的图像,前提是渲染时文本字段在视图中。