I am building an iOS app using Rubymotion. In this app I let the user "sign" their name by drawing on a view. I want to save this "signature" to a real image so that I can upload it to the server.
I think I have found an Objective-C solution here: iPhone : How to save a view as image ??? (ex.save what you draw )
The code for that looks like this:
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
I have tried to "translate" this code into Ruby like this:
UIGraphicsBeginImageContext(signature.bounds.size)
signature.layer.renderInContext(UIGraphicsGetCurrentContext)
image = UIImage.UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
But I get this error when running it:
uninitialized constant SignatureController::UIGraphicsGetCurrentContext (NameError)
2013-01-27 14:20:29.943 buy_app[18384:13d03] *** Terminating app due to uncaught exception 'NameError', reason: 'signature_controller.rb:88:in `save': uninitialized constant SignatureController::UIGraphicsGetCurrentContext (NameError)
What am I doing wrong? What is the best way of saving a normal view into an image?
Thanks!
UPDATE
I solved it like this:
UIGraphicsBeginImageContext(signature.bounds.size)
signature.layer.renderInContext(UIGraphicsGetCurrentContext())
image = UIImage.UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
image = UIImagePNGRepresentation(image)