如何将一个viewController中的图像数据发送到另一个viewController?

时间:2017-07-21 01:17:36

标签: ios swift

我制作了一个时钟应用程序,我想让用户选择几个背景图像。(通过在设置ViewController中单击UIButton,然后在MainViewController中更改背景图像。)所以,我使用segue但是它没有用。这是代码。

let previousCount = self.images.count
self.images.append(newElement) // This is where you are updating your images array

let previousNumberOfRows = (previousCount / 4) + 1 // May want to explicitly check the case where previousCount == 0 if you don't want any rows to show for that case
let updatedNumberOfRows = (self.images.count / 4) + 1 // Again, may want to handle 0 items case
let rowDiff = updatedNumberOfRows - previousNumberOfRows

guard rowDiff != 0 else {
  return
}

let range: CountableRange
if rowDiff > 0 {
  range = (0 ..< rowDiff)
} else {
  range = (rowDiff ..< 0)
}

let indexPathsToReload = range.flatMap { sectionIndex in 
    return IndexPath(row: sectionIndex + previousNumberOfSections, section: 0)
}
self.imagesTableView.reloadRows(at: indexPathsToReload)

我做了一个breakPoint,我意识到当它进入&#39; if&#39;时,它没有检查任何东西而只是退出。我不知道为什么。请给我一个建议。谢谢:))

2 个答案:

答案 0 :(得分:2)

您转换NSData格式的UIImage并添加字典或数组,将其传输到MainViewController。将NSData放在UIImageView中。

答案 1 :(得分:0)

将图像转换为如下数据: -

let data = UIImagePNGRepresentation(image) as? NSData

将数据传递给nextVC: -

 mainVC.imageData = data

显示如下图像: -

let imagePt = UIImage(data: (imageData as! NSData) as Data)