gpt4 book ai didi

ios - 在下一个 View 中显示更大的图像 CollectionView Controller

转载 作者:行者123 更新时间:2023-11-30 13:14:26 24 4
gpt4 key购买 nike

我使用 SDWEBIMAGE 通过 API 在 UICollectionView 中显示图像。现在,当用户点击 Collection View 中的图像时,我想在下一个 View Controller 中打开该图像。我能够在下一个 View 中显示标题,但无法显示图像,因为我希望无法在 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]

在此处输入代码**

最佳答案

第 1 步

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

class DetailViewController: UIViewController {

var MediaStr: String
var label: String


override func viewDidLoad() {
super.viewDidLoad()

print (MediaStr)
}

}

第 2 步

在您的第一次 VC Call Direct 上,例如

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

}

关于ios - 在下一个 View 中显示更大的图像 CollectionView Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38388421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com