我使用SDWEBIMAGE在API中显示UICollectionView中的图像。现在,当用户点击集合视图中的图像时,我想在下一个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]
在这里输入代码**
答案 0 :(得分:0)
<强>步骤1 强>
在DetailViewController
上创建另一个字符串,如MediaStr
class DetailViewController: UIViewController {
var MediaStr: String
var label: String
override func viewDidLoad() {
super.viewDidLoad()
print (MediaStr)
}
}
<强>步骤-2 强>
在您的第一个VC Call 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]
}
<强>步骤-3 强>
用于图像加载目的
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)
}
}
}