将图像从集合视图控制器快速传递到另一个视图控制器
Posted
技术标签:
【中文标题】将图像从集合视图控制器快速传递到另一个视图控制器【英文标题】:swift pass images from collection view controller to another view controller 【发布时间】:2017-06-16 04:02:28 【问题描述】:我已经从 firebase 下载了一组用户照片并将其作为收藏视图查看(类似 instagram) 但我试图放大使用 segue 在另一个视图控制器中单击的照片,但它不起作用。 关于我的代码的任何想法:
class ProfileViewController: UIViewController
var photoThumbnail: UIImage!
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collection_cell", for: indexPath) as! ProfileCollectionViewCell
cell.imageCollection.downloadImage2(from: currPosts[indexPath.row].photoUrl)
cell.imageCollection.image = photoThumbnail
return cell
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
performSegue(withIdentifier: "photoViewSegue", sender: nil)
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
var destViewController : EnlargePhotoViewController = segue.destination as! EnlargePhotoViewController
destViewController.labelText = "test" //**** this works
destViewController.myImage = photoThumbnail //**** this doesnt work
集合视图单元格是:
class ProfileCollectionViewCell: UICollectionViewCell
@IBOutlet weak var imageCollection: UIImageView!
最后是目标viewController:
class EnlargePhotoViewController: UIViewController
@IBOutlet weak var myLabel: UILabel!
@IBOutlet weak var enlargedPhoto: UIImageView!
var labelText = String()
var myImage: UIImage!
override func viewDidLoad()
super.viewDidLoad()
myLabel.text = labelText
enlargedPhoto.image = myImage
【问题讨论】:
*它不是完整的代码,但我分享了最重要的几行 如果没有看到所有代码,很难知道如何回答这个问题,在摘录中提供的变量 photoThumbnail 从未设置并且始终为零。 【参考方案1】:尝试改成这段代码:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
let cell = collectionView.cellForItem(at: indexPath) as! ProfileCollectionViewCell
photoThumbnail = cell.imageCollection.image
performSegue(withIdentifier: "photoViewSegue", sender: nil)
【讨论】:
【参考方案2】:您唯一的目标是获取所选单元格的图像,这可以更有效地完成。你所要做的就是读/写单元格的 imageView 属性,photoThumbnail 永远不会设置所以总是 nil,只需修改这个
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collection_cell", for: indexPath) as! ProfileCollectionViewCell
cell.imageCollection.downloadImage2(from: currPosts[indexPath.row].photoUrl)
//cell.imageCollection.image = photoThumbnail <-- No need of this
return cell
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
// get the correct cell on select
if let cell = collectionView.cellForItem(at: indexPath) as! ProfileCollectionViewCell
photoThumbnail = cell.imageCollection.image <-- finally assign the imageview to image
performSegue(withIdentifier: "photoViewSegue", sender: self)
为了更安全地检索图像,请使用like
override func viewDidLoad()
super.viewDidLoad()
if let getImage = myImage
myLabel.text = labelText
enlargedPhoto.image = getImage
【讨论】:
【参考方案3】:第一个视图控制器
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
if indexPath.row == 0
let objstory = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
objstory.strd = imageView.image
_ = self.navigationController?.pushViewController(objstory, animated: true)
SecondViewController
var strd:UIImage!
@IBOutlet weak var imgprof: UIImageView!
override func viewDidLoad()
super.viewDidLoad()
imgprof.image = strd
// Do any additional setup after loading the view.
【讨论】:
【参考方案4】:class ProfileViewController: UIViewController
var photoThumbnail: UIImage!
var selectedPhotoURL: NSUrl!
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collection_cell", for: indexPath) as! ProfileCollectionViewCell
cell.imageCollection.downloadImage2(from: currPosts[indexPath.row].photoUrl)
cell.imageCollection.image = photoThumbnail
return cell
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
selectedPhotoURL = currPosts[indexPath.row].photoUrl
performSegue(withIdentifier: "photoViewSegue", sender: nil)
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
var destViewController : EnlargePhotoViewController = segue.destination as! EnlargePhotoViewController
destViewController.labelText = "test" //**** this works
//make image from selected url or pass url and download it in EnlargePhotoViewController
let downlodedImage = yourMethodToDownload(selectedPhotoURL)
destViewController.myImage = downlodedImage //**** this will work work
【讨论】:
但是我已经下载了图像,如果我再次下载它,那将是糟糕的内存管理。不过谢谢以上是关于将图像从集合视图控制器快速传递到另一个视图控制器的主要内容,如果未能解决你的问题,请参考以下文章
将 ImageData 从一个视图控制器传递到另一个视图控制器
Swift 4:将数据从集合视图单元传递到另一个没有故事板的视图控制器
当数据从一个视图控制器传递到另一个视图控制器时,我如何能够传递图像?