2016-04-28 70 views
0

我想用segue在兩個視圖之間傳遞UIImage。問題是我的UIImage在第二個視圖控制器中顯示爲白色。用segue傳遞UIImage

的賽格瑞:

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; 
} 

編輯。上的按鈕截圖存儲:

@IBAction func photoAction(sender: AnyObject) { 
    self.screenShot = captureScreen() 
} 

我試過很多的事情,但還是空白(非空事業內存ADRESS是好的)。

回答

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; 
    } 

} 

結果與此代碼:

enter image description here

項目Sample欲瞭解更多信息。

+0

screenshotImage是一個河畔的UIImage。這很奇怪。如果我做svc.toPass = capture()它也可以..但我必須在screenshotImage上存儲。我將使用screenshoteImage存儲編輯帖子 – Makaille

+0

您的'screenshotImage'對象具有任何圖像? –

+0

不錯,這是我的IBAction(我存儲screenShot)的問題是在AnyObject上,而不是在UIbutton上。我認爲它仍然很奇怪,但現在它的工作... – Makaille

0
在第二個場景

你應該,因爲第二個場景修改屬性

var toPass: UIImage! 

您最初的圖像的UIImage()

+0

試過但不工作 – Makaille