Ios 如何将视图另存为图像
我正在使用Rubymotion构建一个iOS应用程序。在这个应用程序中,我让用户通过在视图上绘图来“签名”。我想保存这个“签名”到一个真实的图像,以便我可以上传到服务器 我想我在这里找到了一个Objective-C解决方案: 其代码如下所示:Ios 如何将视图另存为图像,ios,objective-c,rubymotion,Ios,Objective C,Rubymotion,我正在使用Rubymotion构建一个iOS应用程序。在这个应用程序中,我让用户通过在视图上绘图来“签名”。我想保存这个“签名”到一个真实的图像,以便我可以上传到服务器 我想我在这里找到了一个Objective-C解决方案: 其代码如下所示: UIGraphicsBeginImageContext(view.bounds.size); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGr
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsBeginImageContext(signature.bounds.size)
signature.layer.renderInContext(UIGraphicsGetCurrentContext)
image = UIImage.UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIGraphicsBeginImageContext(signature.bounds.size)
signature.layer.renderInContext(UIGraphicsGetCurrentContext())
image = UIImage.UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
image = UIImagePNGRepresentation(image)
我曾尝试将此代码“翻译”为Ruby,如下所示:
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsBeginImageContext(signature.bounds.size)
signature.layer.renderInContext(UIGraphicsGetCurrentContext)
image = UIImage.UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIGraphicsBeginImageContext(signature.bounds.size)
signature.layer.renderInContext(UIGraphicsGetCurrentContext())
image = UIImage.UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
image = UIImagePNGRepresentation(image)
但我在运行它时遇到了以下错误:
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(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsBeginImageContext(signature.bounds.size)
signature.layer.renderInContext(UIGraphicsGetCurrentContext)
image = UIImage.UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIGraphicsBeginImageContext(signature.bounds.size)
signature.layer.renderInContext(UIGraphicsGetCurrentContext())
image = UIImage.UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
image = UIImagePNGRepresentation(image)
我是这样解决的
UIGraphicsBeginImageContext(signature.bounds.size)
signature.layer.renderInContext(UIGraphicsGetCurrentContext())
image = UIImage.UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
image = UIImagePNGRepresentation(image)
我是这样解决的
UIGraphicsBeginImageContext(signature.bounds.size)
signature.layer.renderInContext(UIGraphicsGetCurrentContext())
image = UIImage.UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
image = UIImagePNGRepresentation(image)
对于视网膜显示,您需要使用
UIGraphicsBeginImageContextWithOptions(bounds.size,false,UIScreen.mainScreen.scale)
否则会将图像缩小到1/4分辨率。对于视网膜显示,您需要使用UIGraphicsBeginImageContextWithOptions(bounds.size,false,UIScreen.mainScreen.scale)
否则会将图像缩小到1/4分辨率。