-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
3 changed files
with
55 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// UIImageView+URL.swift | ||
// | ||
// Made with ❤️ by Novum | ||
// | ||
// Copyright © Telefonica. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
extension UIImageView { | ||
/// Loads the urls asynchronously | ||
func load(url: URL, urlForDarkMode: URL? = nil) { | ||
DispatchQueue.global().async { [weak self] in | ||
guard let imageData = try? Data(contentsOf: url) else { return } | ||
if let urlForDarkMode = urlForDarkMode, | ||
let darkImageData = try? Data(contentsOf: urlForDarkMode) { | ||
self?.updateImage(imageData: imageData, darkImageData: darkImageData) | ||
} else { | ||
self?.updateImage(imageData: imageData) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private extension UIImageView { | ||
func updateImage(imageData: Data, darkImageData: Data) { | ||
DispatchQueue.main.async { [weak self] in | ||
self?.image = UIImage(data: imageData) | ||
if let darkImage = UIImage(data: darkImageData) { | ||
self?.image?.imageAsset?.register(darkImage, with: .init(userInterfaceStyle: .dark)) | ||
} | ||
} | ||
} | ||
|
||
func updateImage(imageData: Data) { | ||
DispatchQueue.main.async { [weak self] in | ||
self?.image = UIImage(data: imageData) | ||
} | ||
} | ||
} |