GraphicsContext.swift 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #else
  37. UIGraphicsBeginImageContextWithOptions(size, false, scale)
  38. #endif
  39. }
  40. static func current(size: CGSize, scale: CGFloat, inverting: Bool, cgImage: CGImage?) -> CGContext? {
  41. #if os(macOS)
  42. guard let rep = NSBitmapImageRep(
  43. bitmapDataPlanes: nil,
  44. pixelsWide: Int(size.width),
  45. pixelsHigh: Int(size.height),
  46. bitsPerSample: cgImage?.bitsPerComponent ?? 8,
  47. samplesPerPixel: 4,
  48. hasAlpha: true,
  49. isPlanar: false,
  50. colorSpaceName: .calibratedRGB,
  51. bytesPerRow: 0,
  52. bitsPerPixel: 0) else
  53. {
  54. assertionFailure("[Kingfisher] Image representation cannot be created.")
  55. return nil
  56. }
  57. rep.size = size
  58. guard let context = NSGraphicsContext(bitmapImageRep: rep) else {
  59. assertionFailure("[Kingfisher] Image context cannot be created.")
  60. return nil
  61. }
  62. NSGraphicsContext.current = context
  63. return context.cgContext
  64. #else
  65. guard let context = UIGraphicsGetCurrentContext() else {
  66. return nil
  67. }
  68. if inverting { // If drawing a CGImage, we need to make context flipped.
  69. context.scaleBy(x: 1.0, y: -1.0)
  70. context.translateBy(x: 0, y: -size.height)
  71. }
  72. return context
  73. #endif
  74. }
  75. static func end() {
  76. #if os(macOS)
  77. NSGraphicsContext.restoreGraphicsState()
  78. #else
  79. UIGraphicsEndImageContext()
  80. #endif
  81. }
  82. }