diff --git a/Sources/Networking+Image.swift b/Sources/Networking+Image.swift index 05365d6..8b3ae1a 100644 --- a/Sources/Networking+Image.swift +++ b/Sources/Networking+Image.swift @@ -1,5 +1,8 @@ import Foundation -import UIKit + +#if os(iOS) || os(tvOS) || os(watchOS) + import UIKit +#endif public extension Networking { #if os(iOS) || os(tvOS) || os(watchOS) @@ -9,7 +12,7 @@ public extension Networking { - parameter cacheName: The cache name used to identify the downloaded image, by default the path is used. - parameter completion: A closure that gets called when the image download request is completed, it contains an `UIImage` object and a `NSError`. */ - public func downloadImage(path: String, cacheName: String? = nil, completion: (image: UIImage?, error: NSError?) -> ()) { + public func downloadImage(path: String, cacheName: String? = nil, completion: (image: UIImage?, error: NSError?) -> Void) { let destinationURL: NSURL if let cacheName = cacheName { let replacedPath = cacheName.stringByReplacingOccurrencesOfString("/", withString: "-") @@ -19,10 +22,10 @@ public extension Networking { } else { destinationURL = self.destinationURL(path) } - self.downloadImage(requestURL: self.urlForPath(path), destinationURL: destinationURL, path: path, completion: completion) + self.download(requestURL: self.urlForPath(path), destinationURL: destinationURL, path: path, completion: completion) } - func downloadImage(requestURL requestURL: NSURL, destinationURL: NSURL, path: String, completion: (image: UIImage?, error: NSError?) -> ()) { + func download(requestURL requestURL: NSURL, destinationURL: NSURL, path: String, completion: (image: UIImage?, error: NSError?) -> Void) { if let getFakeRequests = self.fakeRequests[.GET], fakeRequest = getFakeRequests[path] { if fakeRequest.statusCode.statusCodeType() == .Successful, let image = fakeRequest.response as? UIImage { completion(image: image, error: nil) @@ -30,20 +33,20 @@ public extension Networking { let error = NSError(domain: Networking.ErrorDomain, code: fakeRequest.statusCode, userInfo: [NSLocalizedDescriptionKey : NSHTTPURLResponse.localizedStringForStatusCode(fakeRequest.statusCode)]) completion(image: nil, error: error) } - } else if let image = self.imageCache.objectForKey(destinationURL.absoluteString) as? UIImage { + } else if let image = self.cache.objectForKey(destinationURL.absoluteString) as? UIImage { completion(image: image, error: nil) } else if NSFileManager.defaultManager().fileExistsAtURL(destinationURL) { if TestCheck.isTesting { guard let data = NSFileManager.defaultManager().contentsAtPath(destinationURL.path!) else { fatalError("Couldn't get image in destination url: \(destinationURL)") } guard let image = UIImage(data: data) else { fatalError("Couldn't get convert image using data: \(data)") } - self.imageCache.setObject(image, forKey: destinationURL.absoluteString) + self.cache.setObject(image, forKey: destinationURL.absoluteString) completion(image: image, error: nil) } else { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), { guard let data = NSData(contentsOfURL: destinationURL) else { fatalError("Couldn't get image in destination url: \(destinationURL)") } guard let image = UIImage(data: data) else { fatalError("Couldn't get convert image using data: \(data)") } dispatch_async(dispatch_get_main_queue(), { - self.imageCache.setObject(image, forKey: destinationURL.absoluteString) + self.cache.setObject(image, forKey: destinationURL.absoluteString) completion(image: image, error: nil) }) }) @@ -74,7 +77,7 @@ public extension Networking { returnedImage = image data.writeToURL(destinationURL, atomically: true) - self.imageCache.setObject(image, forKey: destinationURL.absoluteString) + self.cache.setObject(image, forKey: destinationURL.absoluteString) } else if let url = url { if let response = response as? NSHTTPURLResponse { returnedError = NSError(domain: Networking.ErrorDomain, code: response.statusCode, userInfo: [NSLocalizedDescriptionKey : NSHTTPURLResponse.localizedStringForStatusCode(response.statusCode)]) diff --git a/Sources/Networking.swift b/Sources/Networking.swift index d2a4c77..96464af 100644 --- a/Sources/Networking.swift +++ b/Sources/Networking.swift @@ -73,7 +73,7 @@ public class Networking { var fakeRequests = [RequestType : [String : FakeRequest]]() var token: String? var customAuthorizationHeader: String? - var imageCache: NSCache + var cache: NSCache var configurationType: ConfigurationType /** @@ -92,7 +92,7 @@ public class Networking { public init(baseURL: String, configurationType: ConfigurationType = .Default, cache: NSCache? = nil) { self.baseURL = baseURL self.configurationType = configurationType - self.imageCache = cache ?? NSCache() + self.cache = cache ?? NSCache() } /** diff --git a/Tests/ImageTests.swift b/Tests/ImageTests.swift index 21df627..f608167 100644 --- a/Tests/ImageTests.swift +++ b/Tests/ImageTests.swift @@ -91,7 +91,7 @@ class ImageTests: XCTestCase { networking.downloadImage(path) { image, error in let destinationURL = networking.destinationURL(path) - XCTAssertNotNil(networking.imageCache.objectForKey(destinationURL.absoluteString)) + XCTAssertNotNil(networking.cache.objectForKey(destinationURL.absoluteString)) } } @@ -104,7 +104,7 @@ class ImageTests: XCTestCase { networking.downloadImage(path, cacheName: cacheName) { image, error in let destinationURL = networking.destinationURL(path, cacheName: cacheName) - XCTAssertNotNil(networking.imageCache.objectForKey(destinationURL.absoluteString)) + XCTAssertNotNil(networking.cache.objectForKey(destinationURL.absoluteString)) } }