2017-06-16 63 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. 
    } 
相關問題