如何使用sd webimage框架swift

时间:2017-05-24 05:17:19

标签: swift uicollectionviewcell sdwebimage

我在第一个视图控制器中有一个集合视图单元,它使用sd webimage第三方库从url获取了imageview。这个图像是一个缩略图。我想将实际图像传递给第一个视图控制器的didselectitemat中的另一个视图控制器。代码如下:

 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)

    }

子类别计数是从url添加数组和默认的数组。有助于我如何将图像从单元格中的url传递到另一个视图控制器。

1 个答案:

答案 0 :(得分:0)

您只需要传递图片网址字符串,而不是 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")
                })
相关问题