如何查看我的所有图像是否已下载?



我正在下载几张图像并缓存它们。然后,我调用缓存的图像并将它们作为子视图添加到我的scrollView(使用页面控制器水平(。当视图出现时,我想同时执行这两项操作,而无需任何其他操作。不幸的是,它在缓存图像之前已经添加了子视图,因为它在下载图像时会遍历代码。因此,不会添加任何内容作为子视图。

当我在视图出现时下载并缓存图像并在按下按钮时将它们添加为子视图时,它可以工作,但我希望它自动工作。

我不确定我的代码是否有助于更好地理解,但就在这里。

这是我下载图像的代码

func downloadImage(url: URL) {
let dataTask = URLSession.shared.dataTask(with: url) { data, responseURL, error in

var downloadedImage:UIImage?

if let data = data {
downloadedImage = UIImage(data: data)
}
// if download actaully got an image
if downloadedImage != nil {
self.sum += 1 
self.cache.setObject(downloadedImage!, forKey: url.absoluteString as NSString) // cache the image

// add the url as value to the dictionary
self.imageURLS[self.sum] = url.absoluteString
}
}
dataTask.resume()
}

这就是我用来添加子视图的内容

func appendImages(){
// sort the values of the dictionary by the greatest
let sortedImageURLs = Array(imageURLS.keys).sorted(by: >)
var stringURLs = [String]() // empty string array to append the URL's

// for loop which goes through all integers in "creation Date" array
for keys in sortedImageURLs {
let url: String? = imageURLS[keys] // url which is connected to the key
stringURLs.append(url!) // append the url to the url array
}
// -> the url string array starts with the latest loaded url

var originSubview = 0 // counter of the origin of the subviews
for urls in stringURLs {
// 1.
frame.origin.x = scrollView.frame.size.width * CGFloat(originSubview)
frame.size = scrollView.frame.size
// 2.
let imageView = UIImageView(frame: frame)

// get the image which is cahed under the url
let image = cache.object(forKey: urls as NSString)

// set the image of image view as the cached image
imageView.image = image
// and add that image view as a subview to the scrollView
scrollView.addSubview(imageView)

// increase counter variable by one
//-> next images origin is at the end of the image before
originSubview += 1
}

// 3.
scrollView.contentSize = CGSize(width: ((scrollView.frame.size.width) * CGFloat(specialsCounter)), height: (scrollView.frame.size.height))
scrollView.delegate = self
pageControl.numberOfPages = specialsCounter
}

您可以使用调度组在所有下载完成时发出通知。 基本上,您在开始下载之前enter(),并在下载完成后leave。 当所有entered任务都left时,将触发notify

这是有关如何执行此操作的示例。我为您的下载图像功能制作了一个完成块。

let dispatchGroup = DispatchGroup()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

for url in imageUrls {
self.dispatchGroup.enter()
downloadImage(url: url) {
self.dispatchGroup.leave()
}
}

self.dispatchGroup.notify(queue: .main) {
//All images has been downloaded here
}

}

下载图像功能与完成:

func downloadImage(url: URL, completion: () -> ()) {
let dataTask = URLSession.shared.dataTask(with: url) { data, responseURL, error in

var downloadedImage:UIImage?
if let data = data {
downloadedImage = UIImage(data: data)
}
// if download actaully got an image
if downloadedImage != nil {
self.sum += 1
self.cache.setObject(downloadedImage!, forKey: url.absoluteString as NSString) // cache the image

// add the url as value to the dictionary
self.imageURLS[self.sum] = url.absoluteString
completion()
} else {
completion()
}
}
dataTask.resume()
}

最新更新