swift - 如何使用prepare for segue 将图像从表格单元格传输到新的 View Controller ?

标签 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

}

最佳答案

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

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

然后尝试将其设置为 UIImage 引用:

upcoming.imageTitle == imageTitle

但是来自 NewViewControllerimageTitle 需要 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)

关于swift - 如何使用prepare for segue 将图像从表格单元格传输到新的 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33775640/

相关文章:

ios - 不同UIApplicationMain时引用appDelegate

javascript - 无法获取指针字段值的值 Parse + React

ios - UITableView push segue 触发多次

swift - Swift 中的自定义 Segue

swift - 结构的下标在创建为隐式展开的可选时不设置值

c# - 在 ASP.net 中从 Swift/iOS 客户端请求接收 POST 数据

ios - 将 CLLocationCoordinate2D 转换为可以存储的字符串

javascript - Uncaught ReferenceError : Parse is not defined

javascript - Parse 上的反向关系查找

ios - 如何通过单元格内的按钮将 NSIndexPath 传递到另一个 ViewController?