0

我正在使用SDWebImage从 API 获取图像。我没有IBOutlet创建 for imageview,而是创建了一个imageview programatically, 并获取webimageusingdownloadImage(with url: URL) 函数imageview。每次单击图像时,都必须不断替换为新图像。问题是,当我单击新图像时,这个新图像会重叠在现有图像上。它不会替换旧图像。我的问题是downloadimage功能是否总是以这种方式工作,或者我这样做的方式是否有任何问题。

if (vtype[i] == "OtherView") && (cattypelink == "Image")
{
    print("i is \(i)")
    imagestrng = demoarray[i]
    self.imgtag = i
    self.tag = self.imgtag
    print("imagestring is sswqqwqew \(imagestrng)")
    let actualURL = URL(string: imagestrng)
    self.downloadImage(with: actualURL!)
}    
func downloadImage(with url:URL) {
    print("inside download image")
    SDWebImageManager.shared().downloadImage(with: url, options: SDWebImageOptions(rawValue: 0), progress: nil) { (image, _, _, _, _) in
        if let _ = image {
            print("image downloaded")
             if self.tag == self.imgtag
             {
              var finalimage: UIImage = image!
              print("finalimage is \(finalimage)")
              self.othersimg = UIImageView(frame: self.oview.frame)
              self.othersimg?.image = nil
              self.othersimg?.image = finalimage
              self.oview = UIView(frame: CGRect(x:0,y:0,width:self.sigeImg.width,height:self.sigeImg.height))
              self.mainview.addSubview(self.oview)
              self.oview.addSubview(self.othersimg!)
              self.othersimg?.tag = self.imgtag
              print("othersimg is \(self.othersimg?.tag)")
            }
            else
             {
              print("nothing")
              }
        }
        else {
            print("not downloaded '")
            // Try to download again (optional)
            self.downloadImage(with: url)
        }
    }
}
4

0 回答 0