GraphicsContext.swift 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // GraphicsContext.swift
  3. // Kingfisher
  4. //
  5. // Created by taras on 19/04/2021.
  6. //
  7. // Copyright (c) 2021 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. #if canImport(AppKit) && !targetEnvironment(macCatalyst)
  27. import AppKit
  28. #endif
  29. #if canImport(UIKit)
  30. import UIKit
  31. #endif
  32. enum GraphicsContext {
  33. static func begin(size: CGSize, scale: CGFloat) {
  34. #if os(macOS)
  35. NSGraphicsContext.saveGraphicsState()
  36. #elseif os(watchOS)
  37. UIGraphicsBeginImageContextWithOptions(size, false, scale)
  38. #else
  39. assertionFailure("This method is deprecated on the current platform and should not be used.")
  40. #endif
  41. }
  42. static func current(size: CGSize, scale: CGFloat, inverting: Bool, cgImage: CGImage?) -> CGContext? {
  43. #if os(macOS)
  44. guard let rep = NSBitmapImageRep(
  45. bitmapDataPlanes: nil,
  46. pixelsWide: Int(size.width),
  47. pixelsHigh: Int(size.height),
  48. bitsPerSample: cgImage?.bitsPerComponent ?? 8,
  49. samplesPerPixel: 4,
  50. hasAlpha: true,
  51. isPlanar: false,
  52. colorSpaceName: .calibratedRGB,
  53. bytesPerRow: 0,
  54. bitsPerPixel: 0) else
  55. {
  56. assertionFailure("[Kingfisher] Image representation cannot be created.")
  57. return nil
  58. }
  59. rep.size = size
  60. guard let context = NSGraphicsContext(bitmapImageRep: rep) else {
  61. assertionFailure("[Kingfisher] Image context cannot be created.")
  62. return nil
  63. }
  64. NSGraphicsContext.current = context
  65. return context.cgContext
  66. #elseif os(watchOS)
  67. guard let context = UIGraphicsGetCurrentContext() else {
  68. return nil
  69. }
  70. if inverting { // If drawing a CGImage, we need to make context flipped.
  71. context.scaleBy(x: 1.0, y: -1.0)
  72. context.translateBy(x: 0, y: -size.height)
  73. }
  74. return context
  75. #else
  76. assertionFailure("This method is deprecated on the current platform and should not be used.")
  77. return nil
  78. #endif
  79. }
  80. static func end() {
  81. #if os(macOS)
  82. NSGraphicsContext.restoreGraphicsState()
  83. #elseif os(watchOS)
  84. UIGraphicsEndImageContext()
  85. #else
  86. assertionFailure("This method is deprecated on the current platform and should not be used.")
  87. #endif
  88. }
  89. }