如何使用prepare for segue将图像从表格单元格传输到newviewcontroller?

时间:2015-11-18 09:02:10

标签: swift parse-platform segue

如何使用prepare for segue将图像从表格单元格传输到Newviewcontroller?我能够传输标签数据而不是图像。我使用parse.com作为我的后端来检索图像。感谢

import UIKit

 class mainVC: UIViewController, UITableViewDataSource, UITableViewDelegate,  {

@IBOutlet weak var resultsTable: UITableView!

var resultsStartdateArray = [String]()
var resultsTweetImageFiles = [PFFile?]()

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return resultsTweetImageFiles.count
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 350
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell:mainCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! mainCell
    cell.dateLbl.text = self.resultsStartdateArray[indexPath.row]
  resultsTweetImageFiles[indexPath.row]?.getDataInBackgroundWithBlock({
            (imageData:NSData?, error:NSError?) -> Void in

            if error == nil {

                let image = UIImage(data: imageData!)
                cell.tweetImg.image = image


            }

        })
    return cell
}

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

let upcoming: NewViewController = segue.destinationViewController as! NewViewController


let indexPath = self.resultsTable.indexPathForSelectedRow!

let titleString = self.resultsStartdateArray[indexPath.row] as String
let imageTitle = self.resultsTweetImageFiles[indexPath.row] as PFFile?// i have tried this but it didnt work 

upcoming.imageTitle == imageTitle // << not working 
upcoming.titleString = titleString

self.resultsTable.deselectRowAtIndexPath(indexPath, animated: true)
}


import UIKit

class NewViewController: UIViewController {

@IBOutlet weak var dateLbl: UILabel!


@IBOutlet weak var tweetImage: UIImageView!

var titleString: String!

var imageTitle: UIImage!

override func viewDidLoad()
{

    super.viewDidLoad()

    self.dateLbl.text = self.titleString

    self.tweetImage.image = self.imageTitle

}

1 个答案:

答案 0 :(得分:0)

在这行代码中,您创建imageTitle作为PFFile对象:

let imageTitle = self.resultsTweetImageFiles[indexPath.row] as PFFile?

然后尝试将其设置为UIImage参考:

upcoming.imageTitle == imageTitle

imageTitle NewViewController期望UIImage类型的图片需要PFFile

的对象,而不是一些不可理解的图像

这就是为什么它不起作用。

要解决此问题,您需要将PFFile对象中的数据转换为UIImage,然后再将其传输到NewViewController对象。您没有编写PFFile对象中包含的数据的表单,因此假设它为NSData格式。在这种情况下,您可以执行以下操作:

if let imageObject = self.resultsTweetImageFiles[indexPath.row] as? PFFile {
    if let imageData = imageObject.getData as? NSData {
        if let image = UIImage(data: imageData) {
            upcoming.imageTitle = image
        }
    }
}

或者,如果您的某个String对象包含base64格式的图片,您可以先将其转换为NSData这样的对象:

let imageData = NSData(base64EncodedString: imageString, options: .IgnoreUnknownCharacters)