Showing
3 changed files
with
71 additions
and
4 deletions
... | @@ -13,6 +13,24 @@ import UIKit | ... | @@ -13,6 +13,24 @@ import UIKit |
13 | @IBOutlet weak var newBadgeImage: UIImageView! | 13 | @IBOutlet weak var newBadgeImage: UIImageView! |
14 | @IBOutlet weak var campaignTitleLabel: UILabel! | 14 | @IBOutlet weak var campaignTitleLabel: UILabel! |
15 | 15 | ||
16 | + var postImageURL: String? { | ||
17 | + didSet { | ||
18 | + if let url = postImageURL { | ||
19 | + self.campaignImage.image = UIImage() // UIImage(named: "loading") | ||
20 | + | ||
21 | + UIImage.loadImageUsingCacheWithUrlString(url) { image in | ||
22 | + // set the image only when we are still displaying the content for the image we finished downloading | ||
23 | + if url == self.postImageURL { | ||
24 | + self.campaignImage.image = image | ||
25 | + } | ||
26 | + } | ||
27 | + } | ||
28 | + else { | ||
29 | + self.campaignImage.image = nil | ||
30 | + } | ||
31 | + } | ||
32 | + } | ||
33 | + | ||
16 | public override func awakeFromNib() { | 34 | public override func awakeFromNib() { |
17 | super.awakeFromNib() | 35 | super.awakeFromNib() |
18 | 36 | ||
... | @@ -43,7 +61,8 @@ import UIKit | ... | @@ -43,7 +61,8 @@ import UIKit |
43 | 61 | ||
44 | 62 | ||
45 | func configureCell(campaign: swiftApi.CampaignItemModel) { | 63 | func configureCell(campaign: swiftApi.CampaignItemModel) { |
46 | - campaignImage.load(link: campaign.logo_url ?? "", placeholder: UIImage(), cache: URLCache()) | 64 | + // campaignImage.load(link: campaign.logo_url ?? "", placeholder: UIImage(), cache: URLCache()) |
65 | + self.postImageURL = campaign.logo_url ?? "" | ||
47 | 66 | ||
48 | campaignTitleLabel.text = campaign.title ?? "" | 67 | campaignTitleLabel.text = campaign.title ?? "" |
49 | 68 | ||
... | @@ -55,7 +74,8 @@ import UIKit | ... | @@ -55,7 +74,8 @@ import UIKit |
55 | } | 74 | } |
56 | 75 | ||
57 | func configureCell(ccms: swiftApi.LoyaltyContextualOfferModel) { | 76 | func configureCell(ccms: swiftApi.LoyaltyContextualOfferModel) { |
58 | - campaignImage.load(link: ccms._imageUrl , placeholder: UIImage(), cache: URLCache()) | 77 | + // campaignImage.load(link: ccms._imageUrl , placeholder: UIImage(), cache: URLCache()) |
78 | + self.postImageURL = ccms._imageUrl | ||
59 | 79 | ||
60 | campaignTitleLabel.text = ccms._title | 80 | campaignTitleLabel.text = ccms._title |
61 | 81 | ... | ... |
... | @@ -16,6 +16,24 @@ import UIKit | ... | @@ -16,6 +16,24 @@ import UIKit |
16 | @IBOutlet weak var subtitleLabel: UILabel! | 16 | @IBOutlet weak var subtitleLabel: UILabel! |
17 | @IBOutlet weak var descriptionLabel: UILabel! | 17 | @IBOutlet weak var descriptionLabel: UILabel! |
18 | 18 | ||
19 | + var postImageURL: String? { | ||
20 | + didSet { | ||
21 | + if let url = postImageURL { | ||
22 | + self.campaignImage.image = UIImage() // UIImage(named: "loading") | ||
23 | + | ||
24 | + UIImage.loadImageUsingCacheWithUrlString(url) { image in | ||
25 | + // set the image only when we are still displaying the content for the image we finished downloading | ||
26 | + if url == self.postImageURL { | ||
27 | + self.campaignImage.image = image | ||
28 | + } | ||
29 | + } | ||
30 | + } | ||
31 | + else { | ||
32 | + self.campaignImage.image = nil | ||
33 | + } | ||
34 | + } | ||
35 | + } | ||
36 | + | ||
19 | public override func awakeFromNib() { | 37 | public override func awakeFromNib() { |
20 | super.awakeFromNib() | 38 | super.awakeFromNib() |
21 | 39 | ||
... | @@ -43,7 +61,8 @@ import UIKit | ... | @@ -43,7 +61,8 @@ import UIKit |
43 | 61 | ||
44 | 62 | ||
45 | func configureCell(campaign: swiftApi.CampaignItemModel) { | 63 | func configureCell(campaign: swiftApi.CampaignItemModel) { |
46 | - campaignImage.load(link: campaign.logo_url ?? "", placeholder: UIImage(), cache: URLCache()) | 64 | + // campaignImage.load(link: campaign.logo_url ?? "", placeholder: UIImage(), cache: URLCache()) |
65 | + self.postImageURL = campaign.logo_url ?? "" | ||
47 | 66 | ||
48 | campaignTitleLabel.text = campaign.title ?? "" | 67 | campaignTitleLabel.text = campaign.title ?? "" |
49 | campaignTitleLabel.font = UIFont(name: "PFSquareSansPro-BoldItalic", size: 18) | 68 | campaignTitleLabel.font = UIFont(name: "PFSquareSansPro-BoldItalic", size: 18) |
... | @@ -60,7 +79,8 @@ import UIKit | ... | @@ -60,7 +79,8 @@ import UIKit |
60 | } | 79 | } |
61 | 80 | ||
62 | func configureCell(ccms: swiftApi.LoyaltyContextualOfferModel) { | 81 | func configureCell(ccms: swiftApi.LoyaltyContextualOfferModel) { |
63 | - campaignImage.load(link: ccms._imageUrl , placeholder: UIImage(), cache: URLCache()) | 82 | + // campaignImage.load(link: ccms._imageUrl , placeholder: UIImage(), cache: URLCache()) |
83 | + self.postImageURL = ccms._imageUrl | ||
64 | 84 | ||
65 | campaignTitleLabel.text = ccms._title | 85 | campaignTitleLabel.text = ccms._title |
66 | campaignTitleLabel.font = UIFont(name: "PFSquareSansPro-BoldItalic", size: 18) | 86 | campaignTitleLabel.font = UIFont(name: "PFSquareSansPro-BoldItalic", size: 18) | ... | ... |
... | @@ -225,6 +225,33 @@ extension UIImageView { | ... | @@ -225,6 +225,33 @@ extension UIImageView { |
225 | } | 225 | } |
226 | } | 226 | } |
227 | 227 | ||
228 | +let imageCache = NSCache<NSString, AnyObject>() | ||
229 | +extension UIImage { | ||
230 | + static func loadImageUsingCacheWithUrlString(_ urlString: String, completion: @escaping (UIImage) -> Void) { | ||
231 | + if let cachedImage = imageCache.object(forKey: urlString as NSString) as? UIImage { | ||
232 | + completion(cachedImage) | ||
233 | + | ||
234 | + } else { | ||
235 | + //No cache, so create new one and set image | ||
236 | + let url = URL(string: urlString) | ||
237 | + URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in | ||
238 | + if let error = error { | ||
239 | + print(error) | ||
240 | + return | ||
241 | + } | ||
242 | + | ||
243 | + DispatchQueue.main.async(execute: { | ||
244 | + if let downloadedImage = UIImage(data: data!) { | ||
245 | + imageCache.setObject(downloadedImage, forKey: urlString as NSString) | ||
246 | + completion(downloadedImage) | ||
247 | + } | ||
248 | + }) | ||
249 | + | ||
250 | + }).resume() | ||
251 | + } | ||
252 | + } | ||
253 | +} | ||
254 | + | ||
228 | typealias GradientPoints = (startPoint: CGPoint, endPoint: CGPoint) | 255 | typealias GradientPoints = (startPoint: CGPoint, endPoint: CGPoint) |
229 | 256 | ||
230 | enum GradientOrientation { | 257 | enum GradientOrientation { | ... | ... |
-
Please register or login to post a comment