Skip to content

Commit

Permalink
Add ImageLoaderDelegate
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Oct 12, 2015
1 parent a1437a3 commit dc159b3
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions Nuke/Source/Core/ImageLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,42 @@ public struct ImageLoaderConfiguration {
}
}

// MARK: - ImageLoaderDelegate

public protocol ImageLoaderDelegate {
func imageLoader(loader: ImageLoader, shouldProcessImage image: Image) -> Bool
func imageLoader(loader: ImageLoader, decompressorForRequest request: ImageRequest) -> ImageProcessing?
}

extension ImageLoaderDelegate {
/** Returns true when image loader should process image. Processing includes both decompressing.
*/
public func imageLoader(loader: ImageLoader, shouldProcessImage image: Image) -> Bool {
return true
}

public func imageLoader(loader: ImageLoader, decompressorForRequest request: ImageRequest) -> ImageProcessing? {
#if !os(OSX)
if request.shouldDecompressImage {
return ImageDecompressor(targetSize: request.targetSize, contentMode: request.contentMode)
}
#endif
return nil
}
}

public class ImageLoaderDefaultDelegate: ImageLoaderDelegate {
public init() {}
}

// MARK: - ImageLoader

/** Implements image loading using objects conforming to ImageDataLoading, ImageDecoding and ImageProcessing protocols. Reuses data tasks for multiple equivalent image tasks.
*/
public class ImageLoader: ImageLoading {
public weak var manager: ImageLoadingManager?
public let configuration: ImageLoaderConfiguration
public let delegate: ImageLoaderDelegate

private var dataLoader: ImageDataLoading {
return self.configuration.dataLoader
Expand All @@ -52,8 +81,9 @@ public class ImageLoader: ImageLoading {
private var sessionTasks = [ImageRequestKey : ImageSessionTask]()
private let queue = dispatch_queue_create("ImageLoader-InternalSerialQueue", DISPATCH_QUEUE_SERIAL)

public init(configuration: ImageLoaderConfiguration) {
public init(configuration: ImageLoaderConfiguration, delegate: ImageLoaderDelegate = ImageLoaderDefaultDelegate()) {
self.configuration = configuration
self.delegate = delegate
}

public func resumeLoadingForTask(task: ImageTask) {
Expand Down Expand Up @@ -118,7 +148,7 @@ public class ImageLoader: ImageLoading {
}

private func processImage(image: Image?, error: ErrorType?, forImageTask imageTask: ImageTask) {
if let image = image, processor = self.processorForRequest(imageTask.request) {
if let image = image, processor = self.processorForRequest(imageTask.request) where self.delegate.imageLoader(self, shouldProcessImage: image) {
let operation = NSBlockOperation { [weak self] in
self?.imageTask(imageTask, didCompleteWithImage: processor.processImage(image), error: error)
}
Expand All @@ -132,8 +162,8 @@ public class ImageLoader: ImageLoading {
private func processorForRequest(request: ImageRequest) -> ImageProcessing? {
var processors = [ImageProcessing]()
#if !os(OSX)
if request.shouldDecompressImage {
processors.append(ImageDecompressor(targetSize: request.targetSize, contentMode: request.contentMode))
if request.shouldDecompressImage, let decompressor = self.delegate.imageLoader(self, decompressorForRequest: request) {
processors.append(decompressor)
}
#endif
if let processor = request.processor {
Expand Down

0 comments on commit dc159b3

Please sign in to comment.