用于加载产生意外结果的UIImage的调度

时间:2018-11-20 06:14:47

标签: ios swift dispatch-queue

我正在构建一个具有多个图像(计划)的应用程序,这些图像已在具有页面内容的滚动视图上加载和设置。我正在尝试在主线程上加载第一个图像,然后在后台线程上从其索引加载周围的图像。所有UIImageViews都位于一个数组(planImages)中,以便在需要使用加载的数据设置其图像时进行引用。

问题是图像并不总是使用最近加载的图像进行更新。这是我第一次使用Dispatch,所以我做错了什么吗?这是给我问题的代码部分。

    func setupPagesInScrollView(firstImage: Int)
{
    let numberOfPages: CGFloat = CGFloat(selectedDetails!.numberOfPlans)

    scrollView!.contentSize = CGSize(width: scrollView!.frame.width * numberOfPages, height: scrollView!.frame.height)
    pageControl!.numberOfPages = selectedDetails!.numberOfPlans

    if(planImages.count < selectedDetails!.numberOfPlans)
    {
        for index in planImages.count...selectedDetails!.numberOfPlans - 1
        {
            let iView = UIImageView()
            iView.frame = CGRect(x: 0, y: 0, width: scrollView!.frame.width, height: scrollView!.frame.height)
            planImages.append(iView)
            scrollView!.addSubview(iView)

            iView.translatesAutoresizingMaskIntoConstraints = false
            iView.topAnchor.constraint(equalTo: scrollView!.topAnchor).isActive = true
            iView.leftAnchor.constraint(equalTo: scrollView!.leftAnchor, constant: scrollView!.frame.width * CGFloat(index)).isActive = true
            iView.heightAnchor.constraint(equalToConstant: scrollView!.contentSize.height).isActive = true
            iView.widthAnchor.constraint(equalToConstant: scrollView!.frame.width).isActive = true
        }
    }

    DispatchQueue.global(qos: .background).async
        {
            //Load first selected image
            let path = self.selectedDetails!.dirPath + self.selectedDetails!.plansName
            self.setupImageAtIndex(path: path, index: firstImage)

            //Build Image data around first image on background threads
            var mask = 1
            let max = self.selectedDetails!.numberOfPlans

            while firstImage + mask < max || firstImage - mask > 0
            {
                if(firstImage + mask < max)
                {
                    self.setupImageAtIndex(path: path, index: firstImage + mask)
                }

                if(firstImage - mask >= 0)
                {
                    self.setupImageAtIndex(path: path, index: firstImage - mask)
                }

                mask = mask + 1
            }

            //Remove extra images from memory if needed
            if(self.planImages.count > self.selectedDetails!.numberOfPlans)
            {
                let dif = self.planImages.count - self.selectedDetails!.numberOfPlans

                for _ in 1...dif
                {
                    let index = self.planImages.count - 1
                    let plan = self.planImages[index]
                    self.planImages.remove(at: index)
                    plan.removeFromSuperview()
                    plan.image = nil
                }
            }
    }
}

private func setupImageAtIndex(path: String, index: Int)
{
    let imageName = index + 1 > 9 ? path + String(index + 1) : path + "0" + String(index + 1)

    planImages[index].image = UIImage(contentsOfFile: imageName)
}

1 个答案:

答案 0 :(得分:0)

根据Apple文档,您必须更新主队列上的UI元素

像这样更新代码

private func setupImageAtIndex(path: String, index: Int)
    {
        let imageName = index + 1 > 9 ? path + String(index + 1) : path + "0" + String(index + 1)
        DispatchQueue.main.async {
            planImages[index].image = UIImage(contentsOfFile: imageName)

        }
    }