I'm looking to save or convert a UIView into an image which can then be emailed as an image.
I've tried :
UIGraphicsBeginImageContext(self.view.bounds.size);
self.view.layer.render(in: UIGraphicsGetCurrentContext()!)
let screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
but then I can't call back screenShot
as it is required in a different function.
func configureMailController() -> MFMailComposeViewController {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setToRecipients(["emailaddress"email.com"])
mailComposerVC.setSubject("ScreenShot")
mailComposerVC.setMessageBody("Here's the image", isHTML: true)
let imageData: NSData = UIImagePNGRepresentation(screenShot.image)!
mail.addAttachmentData(imageData, mimeType: "image/png", fileName: "imageName")
return mailComposerVC
}
I'm not sure if that would work even if it wasn't returning errors, but it's the only way I could think of.