Skip to content

Commit

Permalink
Merge pull request onevcat#773 from Ashok28/master
Browse files Browse the repository at this point in the history
Implemented data download callback
  • Loading branch information
onevcat authored Sep 21, 2017
2 parents 159dbfc + e58d0e5 commit 037c3ec
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion Sources/ImageDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ public protocol ImageDownloaderDelegate: class {
you can implement this method to change that behavior.
*/
func isValidStatusCode(_ code: Int, for downloader: ImageDownloader) -> Bool

/**
Called when the `ImageDownloader` object successfully downloaded image data from specified URL.

- parameter downloader: The `ImageDownloader` object finishes data downloading.
- parameter data: Downloaded data.
- parameter url: URL of the original request URL.

- Note: This callback can be used to preprocess raw image data
before creation of UIImage instance (i.e. decrypting).
*/
func imageDownloader(_ downloader: ImageDownloader, didDownload data: Data, for url: URL) -> Data?
}

extension ImageDownloaderDelegate {
Expand All @@ -141,6 +153,9 @@ extension ImageDownloaderDelegate {
public func isValidStatusCode(_ code: Int, for downloader: ImageDownloader) -> Bool {
return (200..<400).contains(code)
}
public func imageDownloader(_ downloader: ImageDownloader, didDownload data: Data, for url: URL) -> Data? {
return data
}
}

/// Protocol indicates that an authentication challenge could be handled.
Expand Down Expand Up @@ -536,7 +551,12 @@ class ImageDownloaderSessionHandler: NSObject, URLSessionDataDelegate, Authentic

self.cleanFetchLoad(for: url)

let data = fetchLoad.responseData as Data
let data: Data
if let modifiedData = downloader.delegate?.imageDownloader(downloader, didDownload: fetchLoad.responseData as Data, for: url) {
data = modifiedData
} else {
data = fetchLoad.responseData as Data
}

// Cache the processed images. So we do not need to re-process the image if using the same processor.
// Key is the identifier of processor.
Expand Down

0 comments on commit 037c3ec

Please sign in to comment.