我想用两个视图传递两个视图之间的UIImage。问题是我的UIImage在第二个视图控制器中显示为白色。
segue:
var screenShot = UIImage()
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "screenshotEmbed") {
let svc = segue.destinationViewController as! screenshotViewController
svc.toPass = self.screenshot
}
secondViewController:
class screenshotViewController: UIViewController {
@IBOutlet var screenshotView: UIImageView!
var toPass = UIImage()
override func viewDidLoad() {
super.viewDidLoad()
self.screenshotView.image = self.toPass
}
}
我的UIImage只是一个截图,这里的功能是:
func captureScreen() -> UIImage {
var window: UIWindow? = UIApplication.sharedApplication().keyWindow
window = UIApplication.sharedApplication().windows[0]
UIGraphicsBeginImageContextWithOptions(window!.frame.size, window!.opaque, 0.0)
window!.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image;
}
EDIT。屏幕截图存储在按钮上:
@IBAction func photoAction(sender: AnyObject) {
self.screenShot = captureScreen()
}
我尝试过很多东西但仍然空白(不是空的,因为记忆地址很好)。
答案 0 :(得分:1)
不确定您的screenshotImage
对象是否有图片。
我尝试了以下代码并且工作正常。
import UIKit
class ViewController: UIViewController {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "screenshotEmbed") {
let svc = segue.destinationViewController as! ScreenshotViewController
svc.toPass = captureScreen()
}
}
func captureScreen() -> UIImage {
var window: UIWindow? = UIApplication.sharedApplication().keyWindow
window = UIApplication.sharedApplication().windows[0]
UIGraphicsBeginImageContextWithOptions(window!.frame.size, window!.opaque, 0.0)
window!.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image;
}
}
使用此代码的结果:
项目Sample了解更多信息。
答案 1 :(得分:0)
在第二个场景中,你应该将属性修改为
<%= f.input :completed, as: :boolean %>
因为在第二个场景中,您将图像初始化为UIImage()