简而言之,我有一个带有URL的字符串数组:
let images = ["http://www.a.pl/1.jpg", "http://www.a.pl/2.jpg", "http://www.a.pl/3.jpg"]
我如何为每个任务创建任务,并在完成所有任务时得到通知?
答案 0 :(得分:1)
这是一种非常简单的方法,但是:
func didFinishLoadingImages(_ images: [String: UIImage]) {
print(images)
PlaygroundPage.current.needsIndefiniteExecution = false
}
let images = ["http://www.a.pl/1.jpg", "http://www.a.pl/2.jpg", "http://www.a.pl/3.jpg"]
var downloadedImages = [String: UIImage]()
let session = URLSession.shared
var downloadsRemaining = images.count
for imagePath in images {
guard let url = URL(string: imagePath) else {
downloadsRemaining -= 1
continue
}
session.dataTask(with: url) { (data, _, _) in
var image: UIImage?
if let imageData = data {
// Create the image in the background thread to avoid UI performance issues
image = UIImage(data: imageData)
}
DispatchQueue.main.async {
downloadedImages[imagePath] = image
downloadsRemaining -= 1
if downloadsRemaining == 0 {
didFinishLoadingImages(downloadedImages)
}
}
}
.resume()
}
答案 1 :(得分:0)
这是简单的解决方案:
创建ImageAsset
模型和两个别名:
class ImageAsset {
var url: String
var key: String
var image: UIImage?
// MARK: - Initialization
init(url: String, key: String) {
self.url = url
self.key = key
}
}
typealias ImageAssetHandler = ([ImageAsset]) -> Void
typealias ImageHandler = (UIImage?) -> Void
创建将接受图像资产阵列的函数:
func downloadImages(assets: [ImageAsset], completion: @escaping ImageAssetHandler) {
let group = DispatchGroup()
var newAssets = [ImageAsset]()
for asset in assets {
group.enter()
executor.downloadImage(with: asset) { image in
asset.image = image
newAssets.append(asset)
group.leave()
}
}
group.notify(queue: .main) {
completion(newAssets)
}
}
某些执行程序的方法应如下所示:
func downloadImage(with asset: ImageAsset, completion: @escaping ImageHandler) {
Alamofire.request(asset.url).responseImage { response in
completion(response.result.value)
}
}
仅此而已;)
用法示例:
let images = [
"https://i.pinimg.com/originals/d9/07/ec/d907ec461fbfa0f172a1098a0d9e9324.jpg",
"https://c402277.ssl.cf1.rackcdn.com/photos/906/images/story_full_width/sumatran-tiger-circle_44312149.jpg?1345533208",
"https://upload.wikimedia.org/wikipedia/commons/4/41/Siberischer_tiger_de_edit02.jpg"]
network.downloadImages(assets: images.map { ImageAsset(url: $0, key: "hejkey") }) { imageAssets in
print("completed")
for a in imageAssets {
print(a.url)
print(a.key)
print(a.image)
print(a.image?.size)
}
}
并在控制台中显示结果:
completed https://c402277.ssl.cf1.rackcdn.com/photos/906/images/story_full_width/sumatran-tiger-circle_44312149.jpg?1345533208 hejkey Optional( size {333.33333333333331, 200} orientation 0 scale 3.000000) Optional((333.3333333333333, 200.0)) https://upload.wikimedia.org/wikipedia/commons/4/41/Siberischer_tiger_de_edit02.jpg hejkey Optional( size {533.33333333333337, 400} orientation 0 scale 3.000000) Optional((533.3333333333334, 400.0)) https://i.pinimg.com/originals/d9/07/ec/d907ec461fbfa0f172a1098a0d9e9324.jpg hejkey Optional( size {853.33333333333337, 640} orientation 0 scale 3.000000) Optional((853.3333333333334, 640.0))