Skip to content

Commit

Permalink
Merge pull request #22 from kiavashfaisali/fix_watchos6
Browse files Browse the repository at this point in the history
Fixed bugs for watchOS 6 support.
  • Loading branch information
kiavashfaisali committed Nov 29, 2019
2 parents cb646e6 + 00b6d96 commit 48fd12a
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Copyright (c) 2015 Kiavash Faisali. All rights reserved.
//

#if os(iOS)
import UIKit
import MapKit

Expand Down Expand Up @@ -170,3 +171,4 @@ public extension MKAnnotationView {
}
}
}
#endif
2 changes: 2 additions & 0 deletions KFSwiftImageLoader/Extensions/UIButtonExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Copyright (c) 2015 Kiavash Faisali. All rights reserved.
//

#if os(iOS)
import UIKit

// MARK: - UIButton Associated Value Keys
Expand Down Expand Up @@ -259,3 +260,4 @@ public extension UIButton {
}
}
}
#endif
2 changes: 2 additions & 0 deletions KFSwiftImageLoader/Extensions/UIImageViewExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Copyright (c) 2015 Kiavash Faisali. All rights reserved.
//

#if os(iOS)
import UIKit

// MARK: - UIImageView Associated Value Keys
Expand Down Expand Up @@ -210,3 +211,4 @@ public extension UIImageView {
}
}
}
#endif
10 changes: 4 additions & 6 deletions KFSwiftImageLoader/Extensions/WKInterfaceImageExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#if os(watchOS)
import WatchKit


// MARK: - WKInterfaceImage Associated Value Keys
fileprivate var completionAssociationKey: UInt8 = 0

Expand All @@ -32,7 +31,7 @@ public extension WKInterfaceImage {
- parameter placeholderName: `String?` representing the name of a placeholder image that is loaded into the `WKInterfaceImage` while the asynchronous download takes place. The default value is `nil`.
- parameter completion: An optional closure that is called to indicate completion of the intended purpose of this method. It returns two values: the first is a `Bool` indicating whether everything was successful, and the second is `Error?` which will be non-nil should an error occur. The default value is `nil`.
*/
final public func loadImage(urlString: String,
final func loadImage(urlString: String,
placeholderName: String? = nil,
completion: ((_ success: Bool, _ error: Error?) -> Void)? = nil)
{
Expand All @@ -54,7 +53,7 @@ public extension WKInterfaceImage {
- parameter placeholderName: `String?` representing the name of a placeholder image that is loaded into the `WKInterfaceImage` while the asynchronous download takes place. The default value is `nil`.
- parameter completion: An optional closure that is called to indicate completion of the intended purpose of this method. It returns two values: the first is a `Bool` indicating whether everything was successful, and the second is `Error?` which will be non-nil should an error occur. The default value is `nil`.
*/
final public func loadImage(url: URL,
final func loadImage(url: URL,
placeholderName: String? = nil,
completion: ((_ success: Bool, _ error: Error?) -> Void)? = nil)
{
Expand All @@ -73,7 +72,7 @@ public extension WKInterfaceImage {
- parameter placeholderName: `String?` representing the name of a placeholder image that is loaded into the `WKInterfaceImage` while the asynchronous download takes place. The default value is `nil`.
- parameter completion: An optional closure that is called to indicate completion of the intended purpose of this method. It returns two values: the first is a `Bool` indicating whether everything was successful, and the second is `Error?` which will be non-nil should an error occur. The default value is `nil`.
*/
final public func loadImage(request: URLRequest,
final func loadImage(request: URLRequest,
placeholderName: String? = nil,
completion: ((_ success: Bool, _ error: Error?) -> Void)? = nil)
{
Expand All @@ -89,7 +88,7 @@ public extension WKInterfaceImage {
let sharedURLCache = URLCache.shared

// If there's already a cached image, load it into the interface.
if let image = cacheManager[urlAbsoluteString], let imageData = UIImagePNGRepresentation(image) {
if let image = cacheManager[urlAbsoluteString], let imageData = image.pngData() {
self.setImageData(imageData)

self.completion?(true, nil)
Expand Down Expand Up @@ -168,5 +167,4 @@ public extension WKInterfaceImage {
}
}
}

#endif
12 changes: 10 additions & 2 deletions KFSwiftImageLoader/Managers/KFImageCacheManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
// Copyright (c) 2015 Kiavash Faisali. All rights reserved.
//

#if os(iOS)
import UIKit
import MapKit
#endif

#if os(watchOS)
import WatchKit
import WatchKit
#endif

// MARK: - ImageCacheKeys Struct
Expand Down Expand Up @@ -86,12 +88,14 @@ final public class KFImageCacheManager {
// Initialize the disk cache capacity to 50 MB.
let diskURLCache = URLCache(memoryCapacity: 0, diskCapacity: 50 * 1024 * 1024, diskPath: nil)
URLCache.shared = diskURLCache


#if os(iOS)
NotificationCenter.default.addObserver(forName: UIApplication.didReceiveMemoryWarningNotification, object: nil, queue: .main) {
_ in

self.imageCache.removeAll(keepingCapacity: false)
}
#endif
}

deinit {
Expand All @@ -112,12 +116,14 @@ final public class KFImageCacheManager {
if let observerMapping = imageCacheEntry[.observerMapping] as? [NSObject: Int] {
for (observer, initialIndexIdentifier) in observerMapping {
switch observer {
#if os(iOS)
case let imageView as UIImageView:
loadObserver(imageView, image: image, initialIndexIdentifier: initialIndexIdentifier)
case let button as UIButton:
loadObserver(button, image: image, initialIndexIdentifier: initialIndexIdentifier)
case let annotationView as MKAnnotationView:
loadObserver(annotationView, image: image)
#endif
#if os(watchOS)
case let interfaceImage as WKInterfaceImage:
loadObserver(interfaceImage, image: image)
Expand Down Expand Up @@ -181,6 +187,7 @@ final public class KFImageCacheManager {
}

// MARK: - Observer Methods
#if os(iOS)
internal func loadObserver(_ imageView: UIImageView, image: UIImage, initialIndexIdentifier: Int) {
let success = initialIndexIdentifier == imageView.indexPathIdentifier

Expand Down Expand Up @@ -232,6 +239,7 @@ final public class KFImageCacheManager {
annotationView.completion?(true, nil)
}
}
#endif

#if os(watchOS)
internal func loadObserver(_ interfaceImage: WKInterfaceImage, image: UIImage) {
Expand Down
6 changes: 0 additions & 6 deletions KFSwiftImageLoader/Miscellaneous/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
//

import Foundation
import UIKit
import MapKit

#if os(watchOS)
import WatchKit
#endif

// MARK: - AssociatedValue Protocol
internal protocol AssociatedValue {
Expand Down

0 comments on commit 48fd12a

Please sign in to comment.