简体   繁体   中英

How to transfer/display an image from CollectionView to another ViewController

I have looked up many examples and tried to incorporate but have been unsuccessful. In my CollectionView (That has been placed in a ViewController), I'd like to select a cell and push the cell image to another ViewController. The images have been placed in an Array of Dictionaries. I'm not sure, how i should edit both my prepareForSegue or my func collectionView...didSelectItemAtIndexPath. Also, any detailed explanation to go along with your code will be helpful as I'm still learning swift and its syntax.

Below is what i think all the information you need but please let me know if you need more:

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
}

ADDITIONAL CLASS: CollectionViewCell class

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

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

ADDITIONAL CLASS: UIViewController

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

    var story: StoryImages?

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

}

ADDITIONAL CLASS: 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)
   }
 }
}

First pass the indexPath of selectedItem in didSelectItemAtIndexPath

Now pass the image in prepareForSegue method like this

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
    }
}  

Now declare one StoryImages types of object in your StoryView where you want to pass image and use that object in viewDidLoad to assign image

var selectedStory: StoryImages?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM