2017-06-16 134 views
0

我已经下载了一组用户照片从firebase和查看它作为收集视图(Instagram的样子) 但即时通讯尝试放大照片点击另一个视图控制器使用赛格,但其不工作。 我的代码有什么想法:从收集视图控制器迅速传递图像到另一个视图控制器

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 
     } 
    } 
+0

*它不是完整的代码,但我分享最重要的线 –

+0

它很难知道该如何回答这个问题,没有看到所有的代码,在提取物提供的变量photoThumbnail从未设置始终为零。 – user863238

回答

2

尝试改变这样的代码:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    let cell = collectionView.cellForItem(at: indexPath) as! ProfileCollectionViewCell 

    photoThumbnail = cell.imageCollection.image 
    performSegue(withIdentifier: "photoViewSegue", sender: nil) 
} 
+0

太棒了!应该多投 –

+0

伟人,你是最棒的 –

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

} 
+0

但我已经下载了图像,如果我再次下载它将是不好的内存管理。感谢 –

1

您唯一的目标是获得选定单元格的图像,这可以更有效地完成。所有你需要做的是读/写的细胞ImageView的属性,photoThumbnail从未设定所以总是为零,只需修改这个

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

更安全检索图像,使用像

override func viewDidLoad() { 
     super.viewDidLoad() 


     if let getImage = myImage 
     { 
     myLabel.text = labelText 
     enlargedPhoto.image = getImage 
     } 
    } 
0

第一视图控制器

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. 
    } 
相关问题