Created
July 20, 2021 22:47
-
-
Save camiloibarrayepes/b0aef50bb6d9669ddd833d11123ecea9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| let urlImages = ["https://applecoding.com/wp-content/uploads/2019/07/cropped-black-and-white-black-and-white-challenge-262488-1024x576.jpg", | |
| "https://applecoding.com/wp-content/uploads/2019/07/cropped-company-concept-creative-7369-1-1024x575.jpg", | |
| "https://applecoding.com/wp-content/uploads/2018/06/cropped-mapkitjs-portada-1024x576.jpg", | |
| "https://applecoding.com/wp-content/uploads/2019/06/combine-operators-1024x573-1024x576.jpg", | |
| "https://applecoding.com/wp-content/uploads/2019/06/wwdc_2_0.jpg", | |
| "https://applecoding.com/wp-content/uploads/2018/06/header-390x220.jpg", | |
| "https://applecoding.com/wp-content/uploads/2018/06/mapkitjs-portada-1024x576.jpg"] | |
| func downloadImage(url:URL, completionImage: @escaping (UIImage) -> Void) { | |
| URLSession.shared.dataTask(with: url) { data, response, error in | |
| guard let data = data, let response = response as? HTTPURLResponse, error == nil else { | |
| if let error = error { | |
| print("Error en la operación \(error)") | |
| } | |
| return | |
| } | |
| if response.statusCode == 200 { | |
| if let image = UIImage(data: data) { | |
| completionImage(image) | |
| } else { | |
| print("No es una imagen") | |
| } | |
| } else { | |
| print("Error \(response.statusCode)") | |
| } | |
| }.resume() | |
| } | |
| var resultado:[UIImage] = [] | |
| DispatchQueue(label: "com.queue.serial").async { | |
| let downloadGroup = DispatchGroup() | |
| urlImages.forEach { | |
| if let url = URL(string: $0) { | |
| downloadGroup.enter() | |
| downloadImage(url: url) { | |
| resultado.append($0) | |
| downloadGroup.leave() | |
| } | |
| } | |
| } | |
| downloadGroup.wait() | |
| DispatchQueue.main.async { | |
| print(resultado) | |
| resultado | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment