Andriyas  Redel
Andriyas Redel

Reputation: 529

Save altered images to camera roll in Swift 3

The following function saves my background image into the camera roll. Ultimately, it is an image that my custom camera has just taken.

UIImageWriteToSavedPhotosAlbum(self.backgroundImage!, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)

But how do I save an image that has been altered? For example, I put a textfield on it. Which variable should I use instead of backgroundImage? Or should I use a different function like UIGraphicsImageRenderer?

Your help would be highly appreciated.

Upvotes: 3

Views: 831

Answers (1)

Wladek Surala
Wladek Surala

Reputation: 2629

To get an image from current view you can use for example CoreGraphics. Below snippet assumes implementation in view controller and rendering it's whole view:

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
}

where renderedImage is your image with text, provided that text field is in the view at the moment of rendering.

Upvotes: 4

Related Questions