Skip to content

Commit 6cd693f

Browse files
authored
Merge pull request #6 from codecat15/multipart-form-data-format
adding the lazy image view with NSCache implementation
2 parents bc3ef83 + 9658c6a commit 6cd693f

File tree

3 files changed

+42
-31
lines changed

3 files changed

+42
-31
lines changed

HttpUtility.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10-
86171D7C24C7330000D71D06 /* UIImageViewExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86171D7B24C7330000D71D06 /* UIImageViewExtension.swift */; };
10+
86277C2024CB399B0078EB37 /* LazyImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86277C1F24CB399B0078EB37 /* LazyImageView.swift */; };
1111
8656BC582483E3C60023549D /* EncodableExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8656BC572483E3C60023549D /* EncodableExtension.swift */; };
1212
8656BC5B2483E43D0023549D /* EncodableExtensionUnitTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8656BC5A2483E43D0023549D /* EncodableExtensionUnitTest.swift */; };
1313
8656BC5E2484313F0023549D /* HttpUtilityIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8656BC5D2484313F0023549D /* HttpUtilityIntegrationTests.swift */; };
@@ -29,7 +29,7 @@
2929
/* End PBXContainerItemProxy section */
3030

3131
/* Begin PBXFileReference section */
32-
86171D7B24C7330000D71D06 /* UIImageViewExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIImageViewExtension.swift; sourceTree = "<group>"; };
32+
86277C1F24CB399B0078EB37 /* LazyImageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LazyImageView.swift; sourceTree = "<group>"; };
3333
8656BC572483E3C60023549D /* EncodableExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EncodableExtension.swift; sourceTree = "<group>"; };
3434
8656BC5A2483E43D0023549D /* EncodableExtensionUnitTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EncodableExtensionUnitTest.swift; sourceTree = "<group>"; };
3535
8656BC5D2484313F0023549D /* HttpUtilityIntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HttpUtilityIntegrationTests.swift; sourceTree = "<group>"; };
@@ -66,7 +66,7 @@
6666
isa = PBXGroup;
6767
children = (
6868
8656BC572483E3C60023549D /* EncodableExtension.swift */,
69-
86171D7B24C7330000D71D06 /* UIImageViewExtension.swift */,
69+
86277C1F24CB399B0078EB37 /* LazyImageView.swift */,
7070
);
7171
path = Extensions;
7272
sourceTree = "<group>";
@@ -246,9 +246,9 @@
246246
isa = PBXSourcesBuildPhase;
247247
buildActionMask = 2147483647;
248248
files = (
249+
86277C2024CB399B0078EB37 /* LazyImageView.swift in Sources */,
249250
86719EB124720E40002A2AB0 /* HttpUtility.swift in Sources */,
250251
8656BC582483E3C60023549D /* EncodableExtension.swift in Sources */,
251-
86171D7C24C7330000D71D06 /* UIImageViewExtension.swift in Sources */,
252252
);
253253
runOnlyForDeploymentPostprocessing = 0;
254254
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// LazyImageView.swift
3+
// HttpUtility
4+
//
5+
// Created by CodeCat15 on 7/24/20.
6+
// Copyright © 2020 CodeCat15. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import UIKit
11+
12+
class LazyImageView: UIImageView
13+
{
14+
private let imageCache = NSCache<AnyObject, UIImage>()
15+
16+
func loadImage(fromURL imageUrl: URL, placeHolderName: String)
17+
{
18+
self.image = UIImage(named: placeHolderName)
19+
20+
// loading the images from cache
21+
if let cachedImage = imageCache.object(forKey: imageUrl as AnyObject){
22+
self.image = cachedImage
23+
return
24+
}
25+
26+
// if image is not present in the cache then get it from the server
27+
DispatchQueue.global().async { [weak self] in
28+
if let data = try? Data(contentsOf: imageUrl){
29+
if let image = UIImage(data: data){
30+
DispatchQueue.main.async {
31+
self!.imageCache.setObject(image, forKey: imageUrl as AnyObject)
32+
self?.image = image
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}

HttpUtility/Extensions/UIImageViewExtension.swift

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)