0
我試圖將UIImage()
變量的內容傳遞給下一個視圖控制器的單元格。將UIImage傳遞給另一個視圖控制器的單元格
基本上我的佈局是這樣的:
的圖像在ControllerA加載,然後保存在一個模型。
在下一個控制器中,加載一個TableViewCell。
我想要在單元格內加載圖片。
我該如何做到這一點?
我的代碼:
第一部分:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Save the poem.
savePoem()
}
func savePoem() {
// Save the poem current completion date.
poem.date = NSDate()
// Save the theme name for the poem.
poem.theme = theme.name
// Save the currently displayed picture.
poem.image = ImageView.image
poem.words = textField.words
// Make the poem object persist.
PoemPersistence.sharedInstance.persistPoem(poem)
}
細胞加載圖片:
func configureWithPoem(poem: Poem) {
themeLabel.text = "#\(poem.theme)"
poemLabel.text = poem.words
pictureImageView.image = poem.image
}
func capturePoemImage() -> UIImage {
// Hide the share button.
shareButton.hidden = true
// Capture a PNG of the view.
UIGraphicsBeginImageContextWithOptions(frame.size, false, 0)
layer.renderInContext(UIGraphicsGetCurrentContext()!)
let containerViewImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
// Show the share button.
shareButton.hidden = false
return containerViewImage
}
元得以在第二控制器中加載:
func poemCellWantsToSharePoem(poemCell: PoemCell) {
// Find the poem displayed at this index path.
let indexPath = tableView.mp_indexPathForCell(poemCell)
let poem = poems[indexPath.row]
// Generate the image of the poem.
let poemImage = poemCell.capturePoemImage()