UIView+Haneke.swift 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //
  2. // UIView+Haneke.swift
  3. // Haneke
  4. //
  5. // Created by Joan Romano on 15/10/14.
  6. // Copyright (c) 2014 Haneke. All rights reserved.
  7. //
  8. import UIKit
  9. public extension HanekeGlobals {
  10. public struct UIKit {
  11. static func formatWithSize(_ size : CGSize, scaleMode : ImageResizer.ScaleMode, allowUpscaling: Bool = true) -> Format<UIImage> {
  12. let name = "auto-\(size.width)x\(size.height)-\(scaleMode.rawValue)"
  13. let cache = Shared.imageCache
  14. if let (format,_,_) = cache.formats[name] {
  15. return format
  16. }
  17. var format = Format<UIImage>(name: name,
  18. diskCapacity: HanekeGlobals.UIKit.DefaultFormat.DiskCapacity) {
  19. let resizer = ImageResizer(size:size,
  20. scaleMode: scaleMode,
  21. allowUpscaling: allowUpscaling,
  22. compressionQuality: HanekeGlobals.UIKit.DefaultFormat.CompressionQuality)
  23. return resizer.resizeImage($0)
  24. }
  25. format.convertToData = {(image : UIImage) -> Data in
  26. image.hnk_data(compressionQuality: HanekeGlobals.UIKit.DefaultFormat.CompressionQuality) as Data
  27. }
  28. return format
  29. }
  30. public struct DefaultFormat {
  31. public static let DiskCapacity : UInt64 = 50 * 1024 * 1024
  32. public static let CompressionQuality : Float = 0.75
  33. }
  34. static var SetImageAnimationDuration = 0.1
  35. static var SetImageFetcherKey = 0
  36. static var SetBackgroundImageFetcherKey = 1
  37. }
  38. }