如何使用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
}
答案 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)