Skip to content

Commit

Permalink
Merge pull request onevcat#299 from ejmartin504/simplifyMethods
Browse files Browse the repository at this point in the history
Simplify some code using default arguments
  • Loading branch information
onevcat committed Apr 27, 2016
2 parents b698e8d + 9a32c01 commit 89c95c6
Showing 1 changed file with 8 additions and 34 deletions.
42 changes: 8 additions & 34 deletions Sources/ImageCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,34 +144,18 @@ public class ImageCache {
// MARK: - Store & Remove
extension ImageCache {
/**
Store an image to cache. It will be saved to both memory and disk.
It is an async operation, if you need to do something about the stored image, use `-storeImage:forKey:toDisk:completionHandler:`
instead.

- parameter image: The image will be stored.
- parameter originalData: The original data of the image.
Kingfisher will use it to check the format of the image and optimize cache size on disk.
If `nil` is supplied, the image data will be saved as a normalized PNG file.
It is strongly suggested to supply it whenever possible, to get a better performance and disk usage.
- parameter key: Key for the image.
*/
public func storeImage(image: Image, originalData: NSData? = nil, forKey key: String) {
storeImage(image, originalData: originalData, forKey: key, toDisk: true, completionHandler: nil)
}

/**
Store an image to cache. It is an async operation.
Store an image to cache. It will be saved to both memory and disk. It is an async operation.

- parameter image: The image will be stored.
- parameter image: The image to be stored.
- parameter originalData: The original data of the image.
Kingfisher will use it to check the format of the image and optimize cache size on disk.
If `nil` is supplied, the image data will be saved as a normalized PNG file.
It is strongly suggested to supply it whenever possible, to get a better performance and disk usage.
- parameter key: Key for the image.
- parameter toDisk: Whether this image should be cached to disk or not. If false, the image will be only cached in memory.
- parameter completionHandler: Called when stroe operation completes.
- parameter completionHandler: Called when store operation completes.
*/
public func storeImage(image: Image, originalData: NSData? = nil, forKey key: String, toDisk: Bool, completionHandler: (() -> ())?) {
public func storeImage(image: Image, originalData: NSData? = nil, forKey key: String, toDisk: Bool = true, completionHandler: (() -> Void)? = nil) {
memoryCache.setObject(image, forKey: key, cost: image.kf_imageCost)

func callHandlerInMainQueue() {
Expand All @@ -183,7 +167,7 @@ extension ImageCache {
}

if toDisk {
dispatch_async(ioQueue, { () -> Void in
dispatch_async(ioQueue, {
let imageFormat: ImageFormat
if let originalData = originalData {
imageFormat = originalData.kf_imageFormat
Expand Down Expand Up @@ -216,24 +200,14 @@ extension ImageCache {
}

/**
Remove the image for key for the cache. It will be opted out from both memory and disk.
It is an async operation, if you need to do something about the stored image, use `-removeImageForKey:fromDisk:completionHandler:`
instead.

- parameter key: Key for the image.
*/
public func removeImageForKey(key: String) {
removeImageForKey(key, fromDisk: true, completionHandler: nil)
}

/**
Remove the image for key for the cache. It is an async operation.
Remove the image for key for the cache. It will be opted out from both memory and disk.
It is an async operation.

- parameter key: Key for the image.
- parameter fromDisk: Whether this image should be removed from disk or not. If false, the image will be only removed from memory.
- parameter completionHandler: Called when removal operation completes.
*/
public func removeImageForKey(key: String, fromDisk: Bool, completionHandler: (() -> ())?) {
public func removeImageForKey(key: String, fromDisk: Bool = true, completionHandler: (() -> Void)? = nil) {
memoryCache.removeObjectForKey(key)

func callHandlerInMainQueue() {
Expand Down

0 comments on commit 89c95c6

Please sign in to comment.