|
| 1 | +import ObjectiveC |
1 | 2 | import UIKit |
2 | 3 |
|
| 4 | +private var currentURLKey: UInt8 = 0 |
| 5 | +private var placeholderImageKey: UInt8 = 0 |
| 6 | + |
3 | 7 | public extension UIImageView { |
| 8 | + |
| 9 | + private var currentImageURL: String? { |
| 10 | + get { objc_getAssociatedObject(self, ¤tURLKey) as? String } |
| 11 | + set { objc_setAssociatedObject(self, ¤tURLKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } |
| 12 | + } |
| 13 | + |
| 14 | + private var placeholderImage: UIImage? { |
| 15 | + get { objc_getAssociatedObject(self, &placeholderImageKey) as? UIImage } |
| 16 | + set { objc_setAssociatedObject(self, &placeholderImageKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } |
| 17 | + } |
| 18 | + |
4 | 19 | func setPPImage(path: String?) { |
5 | | - guard let path = path else { |
6 | | - self.image = UIImage(named: "image_default") |
| 20 | + loadImageFromImageLoader(path: path, completion: nil) |
| 21 | + } |
| 22 | + |
| 23 | + func setPPImage(path: String?, completion: @escaping () -> Void) { |
| 24 | + loadImageFromImageLoader(path: path, completion: completion) |
| 25 | + } |
| 26 | + |
| 27 | + func loadImageFromImageLoader(path: String?, completion: (() -> Void)? = nil) { |
| 28 | + // ๊ธฐ๋ณธ ์ด๋ฏธ์ง ์ ์ฅ |
| 29 | + if placeholderImage == nil { |
| 30 | + placeholderImage = UIImage(named: "image_default") |
| 31 | + completion?() |
| 32 | + } |
| 33 | + |
| 34 | + guard let path = path, !path.isEmpty else { |
| 35 | + image = placeholderImage |
| 36 | + currentImageURL = nil |
| 37 | + completion?() |
7 | 38 | return |
8 | 39 | } |
| 40 | + |
9 | 41 | let imageURLString = Secrets.popPoolS3BaseURL + path |
10 | | - if let cenvertimageURL = imageURLString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) { |
11 | | - ImageLoader.shared.loadImage(with: cenvertimageURL, defaultImage: UIImage(named: "image_default"), imageQuality: .origin) { [weak self] image in |
12 | | - DispatchQueue.main.async { |
13 | | - self?.image = image |
14 | | - } |
15 | | - } |
| 42 | + guard let encodedURL = imageURLString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { |
| 43 | + image = placeholderImage |
| 44 | + currentImageURL = nil |
| 45 | + completion?() |
| 46 | + return |
16 | 47 | } |
17 | | - } |
18 | 48 |
|
19 | | - func setPPImage(path: String?, completion: @escaping () -> Void) { |
20 | | - guard let path = path else { |
21 | | - self.image = UIImage(named: "image_default") |
22 | | - completion() |
| 49 | + // ์ด๋ฏธ ๊ฐ์ URL์ ๋ก๋ฉํ๊ณ ์ด๋ฏธ์ง๊ฐ ์์ผ๋ฉด ์ฌ๋ก๋ฉ ๋ฐฉ์ง |
| 50 | + if currentImageURL == encodedURL && self.image != nil && self.image != placeholderImage { |
| 51 | + completion?() |
23 | 52 | return |
24 | 53 | } |
25 | | - let imageURLString = Secrets.popPoolS3BaseURL + path |
26 | | - if let cenvertimageURL = imageURLString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) { |
27 | | - let imageURL = URL(string: cenvertimageURL) |
28 | | - ImageLoader.shared.loadImage(with: cenvertimageURL, defaultImage: UIImage(named: "image_default"), imageQuality: .origin) { [weak self] image in |
29 | | - DispatchQueue.main.async { |
30 | | - completion() |
31 | | - self?.image = image |
| 54 | + |
| 55 | + // ํ์ฌ ์ด๋ฏธ์ง URL์ ์
๋ฐ์ดํธ |
| 56 | + currentImageURL = encodedURL |
| 57 | + |
| 58 | + // ๋จผ์ ๋ฉ๋ชจ๋ฆฌ ์บ์ ํ์ธ |
| 59 | + if let cachedImage = MemoryStorage.shared.fetchImage(url: encodedURL) { |
| 60 | + self.image = cachedImage |
| 61 | + completion?() |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + // ๋ค์์ผ๋ก ๋์คํฌ ์บ์ ํ์ธ |
| 66 | + if let diskImage = DiskStorage.shared.fetchImage(url: encodedURL) { |
| 67 | + MemoryStorage.shared.store(image: diskImage, url: encodedURL) |
| 68 | + self.image = diskImage |
| 69 | + completion?() |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + ImageLoader.shared.loadImage(with: encodedURL, defaultImage: placeholderImage, imageQuality: .origin) { [weak self] image in |
| 74 | + guard let self else { return } |
| 75 | + defer { completion?() } |
| 76 | + DispatchQueue.main.async { |
| 77 | + // ํ์ฌ ์์ฒญ ID์ ์บก์ฒ๋ ID๊ฐ ์ผ์นํ๋์ง ํ์ธ (๋ค๋ฅธ ์ด๋ฏธ์ง๋ก ๋ณ๊ฒฝ๋์์ผ๋ฉด ๋ฌด์) |
| 78 | + if self.currentImageURL == encodedURL { |
| 79 | + if let image = image { |
| 80 | + self.image = image |
| 81 | + } else if self.image == nil { |
| 82 | + // ์ด๋ฏธ์ง ๋ก๋ ์คํจ ์ ๊ธฐ๋ณธ ์ด๋ฏธ์ง ํ์ |
| 83 | + self.image = self.placeholderImage |
| 84 | + } |
32 | 85 | } |
33 | 86 | } |
34 | 87 | } |
|
0 commit comments