如何将视图另存为图像

时间:2013-01-27 13:25:54

标签: ios objective-c rubymotion

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

我想我在这里找到了一个Objective-C解决方案:iPhone : How to save a view as image ??? (ex.save what you draw )

代码如下:

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)

1 个答案:

答案 0 :(得分:4)

我解决了这个问题

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