在 CollectionView 的下一个视图控制器中显示更大的图像

Display bigger Image in next view Controller from CollectionView

我使用 SDWEBIMAGE 在 API 的 UICollectionView 中显示图像。现在,当用户点击 collectionview 中的图像时,我想在下一个 viewcontroller 中打开图像。我可以在下一个视图中显示标题,但无法显示图像,因为我不想在 UIImage 上分配它。我正在使用 swift。 任何人都可以建议我如何做。

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]

在此处输入代码**

第一步

DetailViewController 上创建另一个字符串,例如 MediaStr

class DetailViewController: UIViewController {

 var MediaStr: String
  var label: String


override func viewDidLoad() {
    super.viewDidLoad()

    print (MediaStr)
}

}

第 2 步

第一次 VC 像

一样调用 Direclt
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]

}

第三步

用于图像加载目的

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

}