Manos Chorianopoulos

fix cached images issue at gfy, mfy

......@@ -13,6 +13,24 @@ import UIKit
@IBOutlet weak var newBadgeImage: UIImageView!
@IBOutlet weak var campaignTitleLabel: UILabel!
var postImageURL: String? {
didSet {
if let url = postImageURL {
self.campaignImage.image = UIImage() // UIImage(named: "loading")
UIImage.loadImageUsingCacheWithUrlString(url) { image in
// set the image only when we are still displaying the content for the image we finished downloading
if url == self.postImageURL {
self.campaignImage.image = image
}
}
}
else {
self.campaignImage.image = nil
}
}
}
public override func awakeFromNib() {
super.awakeFromNib()
......@@ -43,7 +61,8 @@ import UIKit
func configureCell(campaign: swiftApi.CampaignItemModel) {
campaignImage.load(link: campaign.logo_url ?? "", placeholder: UIImage(), cache: URLCache())
// campaignImage.load(link: campaign.logo_url ?? "", placeholder: UIImage(), cache: URLCache())
self.postImageURL = campaign.logo_url ?? ""
campaignTitleLabel.text = campaign.title ?? ""
......@@ -55,7 +74,8 @@ import UIKit
}
func configureCell(ccms: swiftApi.LoyaltyContextualOfferModel) {
campaignImage.load(link: ccms._imageUrl , placeholder: UIImage(), cache: URLCache())
// campaignImage.load(link: ccms._imageUrl , placeholder: UIImage(), cache: URLCache())
self.postImageURL = ccms._imageUrl
campaignTitleLabel.text = ccms._title
......
......@@ -16,6 +16,24 @@ import UIKit
@IBOutlet weak var subtitleLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!
var postImageURL: String? {
didSet {
if let url = postImageURL {
self.campaignImage.image = UIImage() // UIImage(named: "loading")
UIImage.loadImageUsingCacheWithUrlString(url) { image in
// set the image only when we are still displaying the content for the image we finished downloading
if url == self.postImageURL {
self.campaignImage.image = image
}
}
}
else {
self.campaignImage.image = nil
}
}
}
public override func awakeFromNib() {
super.awakeFromNib()
......@@ -43,7 +61,8 @@ import UIKit
func configureCell(campaign: swiftApi.CampaignItemModel) {
campaignImage.load(link: campaign.logo_url ?? "", placeholder: UIImage(), cache: URLCache())
// campaignImage.load(link: campaign.logo_url ?? "", placeholder: UIImage(), cache: URLCache())
self.postImageURL = campaign.logo_url ?? ""
campaignTitleLabel.text = campaign.title ?? ""
campaignTitleLabel.font = UIFont(name: "PFSquareSansPro-BoldItalic", size: 18)
......@@ -60,7 +79,8 @@ import UIKit
}
func configureCell(ccms: swiftApi.LoyaltyContextualOfferModel) {
campaignImage.load(link: ccms._imageUrl , placeholder: UIImage(), cache: URLCache())
// campaignImage.load(link: ccms._imageUrl , placeholder: UIImage(), cache: URLCache())
self.postImageURL = ccms._imageUrl
campaignTitleLabel.text = ccms._title
campaignTitleLabel.font = UIFont(name: "PFSquareSansPro-BoldItalic", size: 18)
......
......@@ -225,6 +225,33 @@ extension UIImageView {
}
}
let imageCache = NSCache<NSString, AnyObject>()
extension UIImage {
static func loadImageUsingCacheWithUrlString(_ urlString: String, completion: @escaping (UIImage) -> Void) {
if let cachedImage = imageCache.object(forKey: urlString as NSString) as? UIImage {
completion(cachedImage)
} else {
//No cache, so create new one and set image
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in
if let error = error {
print(error)
return
}
DispatchQueue.main.async(execute: {
if let downloadedImage = UIImage(data: data!) {
imageCache.setObject(downloadedImage, forKey: urlString as NSString)
completion(downloadedImage)
}
})
}).resume()
}
}
}
typealias GradientPoints = (startPoint: CGPoint, endPoint: CGPoint)
enum GradientOrientation {
......