ios - 如何将图像从 CollectionView 传输/显示到另一个 ViewController

标签 ios swift segue viewcontroller collectionview

我查阅了很多例子并尝试合并但没有成功。在我的 CollectionView(已放置在 ViewController 中)中,我想选择一个单元格并将单元格图像推送到另一个 ViewController。图像已放置在字典数组中。我不确定,我应该如何编辑我的 prepareForSegue 或我的 func collectionView...didSelectItemAtIndexPath。此外,任何与您的代码一起使用的详细解释都会有所帮助,因为我仍在学习 swift 及其语法。

以下是我认为您需要的所有信息,但如果您需要更多信息,请告诉我:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "ShowToStory") {
        var story = sender as! UICollectionViewCell, indexPath = collectionView.indexPathForCell(story)

    }
}

private func initStoryImages() {

    var storyArchives = [StoryImages]()
    let inputFile = NSBundle.mainBundle().pathForResource("StoryArchive", ofType: "plist")

    let inputDataArray = NSArray(contentsOfFile: inputFile!)

    for inputItem in inputDataArray as! [Dictionary<String, String>] {

        let storyImage = StoryImages(dataDictionary: inputItem)
        storyArchives.append(storyImage)       
}
      storyImages = storyArchives
}

附加类:CollectionViewCell 类

class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var cellImage: UIImageView!

func setStoryImage(item:StoryImages){
cellImage.image = UIImage(named:item.itemImage)
}
}

附加类:UIViewController

class StoryView: UIViewController{
@IBOutlet weak var ImageToStory: UIImageView!

    var story: StoryImages?

override func viewDidLoad() {
    super.viewDidLoad()
    ImageToStory.image = UIImage(named: (story?.itemImage)!)
}

}

附加类:StoryImages

class StoryImages{

var itemImage: String


init(dataDictionary:Dictionary <String,String>) {
itemImage = dataDictionary["ItemImage"]!
}

class func newStoryImage(dataDictionary:Dictionary<String,String>) -> StoryImages {
return StoryImages(dataDictionary: dataDictionary)
   }
 }
}

最佳答案

首先在didSelectItemAtIndexPath中传入selectedItem的indexPath

现在像这样在 prepareForSegue 方法中传递图像

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "ShowToStory") {
        let cell = sender as! UICollectionViewCell //Change UICollectionViewCell with CustomCell name if you are using CustomCell
        let indexPath = self.collectionView?.indexPathForCell(cell) 
        let story = storyImages[indexPath.row]
        let destVC = segue.destinationViewController as! StoryView
        destVC.selectedStory = story
    }
}  

现在在您要传递图像的 StoryView 中声明一个 StoryImages 类型的对象,并在 viewDidLoad 中使用该对象来分配图像

var selectedStory: StoryImages?

override func viewDidLoad() {
    super.viewDidLoad()
    ImageToStory.image = selectedStory.itemImage // or use NSData if it contain url string
}

关于ios - 如何将图像从 CollectionView 传输/显示到另一个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38526133/

相关文章:

ios - iOS/swift中通过TableView显示静态内容

ios - 带数组 iOS 的 JSON 字符串

swift - 删除数组中多个索引处的项目

swift - 扩展的静态函数的派生类型

swift - 如何在 SwiftUI 中创建带有图像的按钮?

xcode - 在 xcode 中推送 segue,没有动画

ios - UIButton 以及 IBAction 和 segue 的顺序

ios - Apple HealthKit 葡萄糖水平读数出现转换错误

ios - 未找到 ios 开发证书且未找到未过期的配置文件

ios - Swift:CGPathRelease 和 ARC