Skip to content

在5.14.0开始出现两个imageView加载同一个url,可能导致第二个ImageView没有展示图片 #3475

@fangandyuan

Description

@fangandyuan

测试代码

import UIKit
import SDWebImage
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let image = UIImage(named: "test111")
        
        saveImage(currentImage: image!, persent: 1.0, imageName: "test111")
    }
    
    private func saveImage(currentImage: UIImage, persent: CGFloat, imageName: String){
        if let imageData = currentImage.jpegData(compressionQuality: persent) as NSData? {
                let fullPath = NSHomeDirectory().appending("/Documents/").appending(imageName)
                imageData.write(toFile: fullPath, atomically: true)
                print("fullPath=\(fullPath)")
                addImageViews(fullPath:fullPath)
            }
        }
    
    func newImageView() -> UIImageView {
        let imageView = SDAnimatedImageView()
        imageView.contentMode = .scaleAspectFit
        imageView.backgroundColor = .yellow
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.isUserInteractionEnabled = true
        imageView.sd_imageIndicator = SDWebImageActivityIndicator.gray
        imageView.sd_imageIndicator?.startAnimatingIndicator()
        return imageView
    }

    var i = 1
    var j = 1
    func addImageViews(fullPath: String) {
        DispatchQueue.main.async {
            let imageView = self.newImageView()
            self.view.addSubview(imageView)
            imageView.frame = CGRect(x: 50 + self.j * 22, y: 60 + 22 * self.i, width: 20, height: 20)
            let url = URL(fileURLWithPath: fullPath)
            imageView.sd_setImage(with: url) {image, error, cacheType, url in
                print("+++++++++++\(String(describing: image))")
                imageView.image = image
            }
            self.i += 1
            if self.i < 30 {
                self.addImageViews(fullPath:fullPath)
            } else if self.j < 4 {
                self.i = 1
                self.j += 1
                self.addImageViews(fullPath:fullPath)
            }
        }
    }
}

发现问题可能出在

for (SDWebImageDownloaderOperationToken *token in tokens)
{
}

可能在解码这段时间内,tokens数组增加了新的token,但是新来的token不会被创建Operation。
之前的版本出现的概率较低,是因为之前版本都是先生成图片,再获取completed数组,去执行

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions