Skip to content

Commit

Permalink
Merge pull request #79 from gigigoapps/hotfix/v3.4.3
Browse files Browse the repository at this point in the history
Fix: Bug in ImageDownloader fixed
  • Loading branch information
radaloda authored Nov 13, 2019
2 parents 8cb198f + b4d9645 commit dde25ea
Showing 1 changed file with 66 additions and 65 deletions.
131 changes: 66 additions & 65 deletions Source/GIGUtils/Image/ImageDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@
import UIKit

struct ImageDownloader {
static let shared = ImageDownloader()
static var queue: [UIImageView: Request] = [:]
static var stack: [UIImageView] = []
static var images: [String: UIImage] = [:]
private init() {
static let shared = ImageDownloader()
static var queue: [UIImageView: Request] = [:]
static var stack: [UIImageView] = []
static var images: [String: UIImage] = [:]
private init() {
NotificationCenter.default.addObserver(forName: UIApplication.didReceiveMemoryWarningNotification, object: nil, queue: nil) { _ in
ImageDownloader.images = [:]
ImageDownloader.stack = []
ImageDownloader.queue = [:]
}
}
// MARK: - Public methods
func download(url: String, for view: UIImageView, placeholder: UIImage?) {
if let request = ImageDownloader.queue[view] {
request.cancel()
ImageDownloader.queue.removeValue(forKey: view)
}
ImageDownloader.images = [:]
ImageDownloader.stack = []
ImageDownloader.queue = [:]
}
}
// MARK: - Public methods
func download(url: String, for view: UIImageView, placeholder: UIImage?) {
if let request = ImageDownloader.queue[view] {
request.cancel()
ImageDownloader.queue.removeValue(forKey: view)
}
if let image = ImageDownloader.images[url] {
DispatchQueue.main.async {
view.image = image
Expand All @@ -44,28 +44,29 @@ struct ImageDownloader {
}
}

// MARK: - Private Helpers
private func loadImage(url: String, in view: UIImageView) {
let request = Request(method: HTTPMethod.get.rawValue, baseUrl: url, endpoint: "")
ImageDownloader.queue[view] = request
ImageDownloader.stack.append(view)
if ImageDownloader.stack.count <= 3 {
self.downloadNext()
}
}
// MARK: - Private Helpers
private func loadImage(url: String, in view: UIImageView) {
let request = Request(method: HTTPMethod.get.rawValue, baseUrl: url, endpoint: "")
ImageDownloader.queue[view] = request
ImageDownloader.stack.append(view)
if ImageDownloader.stack.count <= 3 {
self.downloadNext()
}
}

private func downloadNext() {
guard let view = ImageDownloader.stack.popLast() else { return }
guard let request = ImageDownloader.queue[view] else {
private func downloadNext() {
guard let view = ImageDownloader.stack.popLast() else { return }
guard let request = ImageDownloader.queue[view] else {
return
}

request.fetch { response in
switch response.status {

case .success:

request.fetch { response in
switch response.status {

case .success:

if let image = try? response.image() {
DispatchQueue(label: "com.gigigo.imagedownloader", qos: .background).async {
var finalImage = image
Expand All @@ -74,10 +75,10 @@ struct ImageDownloader {
if let resized = image.imageProportionally(with: CGSize(width: width, height: height)) {
finalImage = resized
}
ImageDownloader.images.updateValue(finalImage, forKey: request.baseURL)



DispatchQueue.main.async {
ImageDownloader.images.updateValue(finalImage, forKey: request.baseURL)

if let currentRequest = ImageDownloader.queue[view], request.baseURL == currentRequest.baseURL {
self.setAnimated(image: finalImage, in: view)
}
Expand All @@ -90,7 +91,7 @@ struct ImageDownloader {
} else if let imageGif = try? response.gif() {
DispatchQueue.main.async {
ImageDownloader.images.updateValue(imageGif, forKey: request.baseURL)

if let currentRequest = ImageDownloader.queue[view], request.baseURL == currentRequest.baseURL {
self.setAnimated(image: imageGif, in: view)
}
Expand All @@ -104,24 +105,24 @@ struct ImageDownloader {
LogWarn("Al descargar la imagen,o se ha recibido un body vacio o no se se ha reconocido el tipo de imagen que es.")
self.downloadNext()
}
default:
LogError(response.error)
self.downloadNext()
}
}
}
private func setAnimated(image: UIImage?, in view: UIImageView) {
UIView.transition(
with: view,
duration:0.4,
options: .transitionCrossDissolve,
animations: {
view.image = image
},
completion: nil
)
}
default:
LogError(response.error)
self.downloadNext()
}
}
}
private func setAnimated(image: UIImage?, in view: UIImageView) {
UIView.transition(
with: view,
duration:0.4,
options: .transitionCrossDissolve,
animations: {
view.image = image
},
completion: nil
)
}
}

0 comments on commit dde25ea

Please sign in to comment.