0

我正在使用 Rubymotion 构建一个 iOS 应用程序。在这个应用程序中,我让用户通过在视图上绘制来“签名”他们的名字。我想将此“签名”保存为真实图像,以便可以将其上传到服务器。

我想我在这里找到了一个 Objective-C 解决方案:iPhone : How to save a view as image ??? (例如保存你画的东西)

代码如下所示:

UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

我试图将这段代码“翻译”成 Ruby,如下所示:

UIGraphicsBeginImageContext(signature.bounds.size)
signature.layer.renderInContext(UIGraphicsGetCurrentContext)
image = UIImage.UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

但是运行它时出现此错误:

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)

我究竟做错了什么?将普通视图保存到图像中的最佳方法是什么?

谢谢!

更新

我是这样解决的:

UIGraphicsBeginImageContext(signature.bounds.size)
signature.layer.renderInContext(UIGraphicsGetCurrentContext())
image = UIImage.UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
image = UIImagePNGRepresentation(image)
4

1 回答 1

4

我这样解决了

UIGraphicsBeginImageContext(signature.bounds.size)
signature.layer.renderInContext(UIGraphicsGetCurrentContext())
image = UIImage.UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
image = UIImagePNGRepresentation(image)
于 2013-01-29T13:44:37.330 回答