2017-05-24 1 views
0

私はsd webimageサードパーティ製ライブラリを使用してurlからイメージビューを取得した最初のビューコントローラにコレクションビューセルを持っています。この画像はサムネイル画像です。sd webimage framework swiftを使用してurlから別のview contollerに画像を渡す方法

func numberOfSections(in collectionView: UICollectionView) -> Int { 
     return 1 
    } 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
      return subcategoryArray.count 

    } 
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     print("inside cell for item at ") 

     let cell:SubCategoryCollectionViewCell = self.collectionview3.dequeueReusableCell(withReuseIdentifier: "Cell2", for: indexPath) as! SubCategoryCollectionViewCell 

     if defaultAnimalArray.count - 1 >= indexPath.row 
     { 
      let item = self.defaultAnimalArray[indexPath.row] 
      cell.subThumbImg?.image = UIImage(named: item as! String) 
     } 

     else 
     { 
      //If now defaultAnimalArray.count = 8, indexPath = 8 , But array = 0...4, then, 
      let item1 = self.subcategoryArray[indexPath.row - self.defaultAnimalArray.count] 
      self.subcatthumbimagelink = (item1 as AnyObject).object(forKey: "SubcategoryThumb") as! String 
      cell.subThumbImg.sd_setImage(with: URL(string: self.subcatthumbimagelink), placeholderImage: UIImage(named: "placeholder.png"),options: SDWebImageOptions(), completed: {(image, error, cacheType, imageURL) -> Void in 
       print("image loaded") 
      }) 
      } 


     return cell 

    } 
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     print("image selected for coloring") 


    if defaultAnimalArray.count - 1 >= indexPath.row 
    { 
     print("indexpath selected is \(indexPath.row)") 
     let item = self.animalcategoryImages[indexPath.row] 
     var drawVC = self.storyboard?.instantiateViewController(withIdentifier: "DrawingViewController") as! DrawingViewController 
     drawVC.selectedimage = UIImage(named:item) 
     self.navigationController?.pushViewController(drawVC, animated: true) 
    } 

    else 
    { 
     print("indexpath selected in else loop is \(indexPath.row)") 


    let item1 = self.subcategoryArray[indexPath.row - 10] 
     print("subcategory count after manipulation is \(self.subcategoryArray)") 
     print("count of item1 \((item1 as AnyObject).count)") 
     print("item1 is \(item1)") 
     self.subcategoryimagelink = (item1 as AnyObject).object(forKey: "SubcategoryImage") as! String 
     print("category image link is \(self.subcategoryimagelink)") 
     self.ImageviewMain.sd_setImage(with: URL(string: self.subcategoryimagelink)) 

     var drawVC = self.storyboard?.instantiateViewController(withIdentifier: "DrawingViewController") as! DrawingViewController 
     drawVC.selectedimage = self.ImageviewMain.image 
     self.navigationController?.pushViewController(drawVC, animated: true) 

    } 

サブカテゴリカウントがdefault.Kindlyどのように助けているURLと、アレイからアレイの付加である:次のように最初のビューcontroller.Theコードのdidselectitematに別のビューコントローラに実際の画像を渡すことですセルにあるURLから別のView Controllerに画像を渡します。

+0

の画像のみURL文字列を渡す必要があります次のビューコントローラ。ユーザーが別のビューコントローラーにリダイレクトすると、イメージの非同期呼び出しによってイメージビューにイメージがロードされず、次のビューコントローラーに実際のイメージを表示できなくなる可能性が高いためです。 –

答えて

0

あなたがへのURLの代わりに、画像を渡す必要があります代わりにUIImageオブジェクト私の意見では

var drawVC = self.storyboard?.instantiateViewController(withIdentifier: "DrawingViewController") as! DrawingViewController 
drawVC.imageURL = self.subcatthumbimagelink // you can also pass string from array 
self.navigationController?.pushViewController(drawVC, animated: true) 

DrawingViewControllerで

var imageURL : String? 
self.imageView.sd_setImage(with: URL(string:imageURL), placeholderImage: UIImage(named: "placeholder.png"),options: SDWebImageOptions(), completed: {(image, error, cacheType, imageURL) -> Void in 
        print("image loaded") 
       }) 
+0

次のView Controllerにはimageviewはありません。UIImageはscrollviewの上にのみあります。 –

+0

この画像を表示したくないのですか? – KKRocks

+0

表示されない場合は、イメージを次のように表示できます。** let data = try? – KKRocks

関連する問題