简体   繁体   中英

Display bigger Image in next view Controller from CollectionView

I used SDWEBIMAGE to display images in a UICollectionView from an API. Now, When the user taps on the image in a collectionview, i want to open the image in the next viewcontroller. I am able to display the title in the next View, but couldn't display the image because i want not able to assign it at UIImage. I am using swift. can anyone please suggest me a way on how to do it.

import UIKit


import SDWebImage


private let reuseIdentifier = "Celll"

 var titles = [String]()

var imagecollection = [String]()

class CollectionViewController: UICollectionViewController, 

UICollectionViewDelegateFlowLayout {

let sectioninserts = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
var titles = [String]()
var imagecollection = [String]()
override func viewDidLoad() {
    super.viewDidLoad()

    let url = NSURL(string: "https://api.myjson.com/bins/537mf")!
    let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) in
        if error != nil {
            print("error")

        }else {
            if let urlcontent = data {
                do {
              let jsoncontent = try NSJSONSerialization.JSONObjectWithData(urlcontent, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
                   // print(jsoncontent)

                    if jsoncontent.count > 0 {

                        let items = jsoncontent["items"] as! NSArray

                        for item in items as! [[String:String]]{

                            self.imagecollection.append(item["media"]!)
                          print(self.imagecollection)
                            self.titles.append(item["title"]!)
                          print(self.titles)


                        }

                        dispatch_async(dispatch_get_main_queue(), {
                            self.collectionView?.reloadData()
                        })

                    }




                } catch {}
            }
        }
    }
    task.resume()


override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    return imagecollection.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell

    cell.titleee.text = self.titles[indexPath.row]
    let imagestring = imagecollection[indexPath.row]
    let imageurl = NSURL(string: imagestring)
    cell.disp.sd_setImageWithURL(imageurl, placeholderImage: UIImage(named: "loading.gif"), options: SDWebImageOptions.ProgressiveDownload, completed: nil)




    return cell
}
func collectionView(collectionView: UICollectionView!,
                    layout collectionViewLayout: UICollectionViewLayout!,
                           sizeForItemAtIndexPath indexPath: NSIndexPath!) -> CGSize {
    return CGSize(width: 170, height: 300)
}




func collectionView(collectionView: UICollectionView!,
                    layout collectionViewLayout: UICollectionViewLayout!,
                           insetForSectionAtIndex section: Int) -> UIEdgeInsets {
    return sectioninserts
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "detail" {
        let cell = sender as! CollectionViewCell
        let indexPath = collectionView?.indexPathForCell(cell)
        let vc = segue.destinationViewController as! DetailViewController


        vc.label = self.titles[indexPath!.row]

enter code here**

Step-1

on that DetailViewController create the another one String like MediaStr

class DetailViewController: UIViewController {

 var MediaStr: String
  var label: String


override func viewDidLoad() {
    super.viewDidLoad()

    print (MediaStr)
}

}

Step-2

on your first VC Call Direclt like

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if segue.identifier == "detail" {
    let cell = sender as! CollectionViewCell
    let indexPath = collectionView?.indexPathForCell(cell)
    let vc = segue.destinationViewController as! DetailViewController


    vc.label = self.titles[indexPath!.row]
   // add the folloing line

    vc.MediaStr = self. imagecollection[indexPath!.row]

}

Step-3

for image loading purpose

import SDWebImage

class DetailViewController: UIViewController {

 var MediaStr: String
  var label: String


override func viewDidLoad() {
    super.viewDidLoad()

    print (MediaStr)

   if let imagestring = MediaStr
   {


yourImageViewName_setImageWithURL(NSURL(string: imagestring), placeholderImage: UIImage(named: "loading.gif"), options: SDWebImageOptions.ProgressiveDownload, completed: nil)
   }
}

}

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