NSImage+Extension.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // NSImage+Extension.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/10/23.
  6. //
  7. import Foundation
  8. extension NSImage {
  9. //MARK: - 修改图片颜色
  10. func filled(with color: NSColor) -> NSImage? {
  11. let tintedImage = NSImage(size: self.size)
  12. tintedImage.lockFocus()
  13. let rect = NSRect(origin: .zero, size: self.size)
  14. color.setFill()
  15. rect.fill()
  16. self.draw(in: rect,
  17. from: NSZeroRect,
  18. operation: .destinationIn,
  19. fraction: 1.0)
  20. tintedImage.unlockFocus()
  21. return tintedImage
  22. }
  23. //MARK: - 组合图片
  24. static func combineImages(images:[NSImage?]) -> NSImage? {
  25. var maxWidth: CGFloat = 0
  26. var maxHeight: CGFloat = 0
  27. for image in images {
  28. maxWidth = max(maxWidth, image!.size.width)
  29. maxHeight = max(maxHeight, image!.size.height)
  30. }
  31. let size = CGSize(width: maxWidth, height: maxHeight)
  32. let combinedImage = NSImage(size: size)
  33. combinedImage.lockFocus()
  34. for image in images {
  35. let rect = NSRect(origin: .zero, size: size)
  36. image!.draw(in: rect, from: rect, operation: .sourceOver, fraction: 1.0)
  37. }
  38. combinedImage.unlockFocus()
  39. return combinedImage
  40. }
  41. static func colorWithImage(baseImage: NSImage, frameImage: NSImage, withColor theColor: NSColor) -> NSImage {
  42. if let ciImage = CIImage(bitmapImageRep: NSBitmapImageRep(data: baseImage.tiffRepresentation!)!),
  43. let size = ciImage.extent.size as? CGSize {
  44. guard let ctx = CGContext(data: nil,
  45. width: Int(size.width * 4),
  46. height: Int(size.height * 4),
  47. bitsPerComponent: 8,
  48. bytesPerRow: 8 * Int(size.width * 4),
  49. space: CGColorSpaceCreateDeviceRGB(),
  50. bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else {
  51. return NSImage()
  52. }
  53. let red = UnsafeMutablePointer<CGFloat>.allocate(capacity: 1)
  54. let green = UnsafeMutablePointer<CGFloat>.allocate(capacity: 1)
  55. let blue = UnsafeMutablePointer<CGFloat>.allocate(capacity: 1)
  56. let alpha = UnsafeMutablePointer<CGFloat>.allocate(capacity: 1)
  57. let area = CGRect(x: 0, y: 0, width: size.width * 4, height: size.height * 4)
  58. ctx.saveGState()
  59. ctx.clip(to: area, mask: baseImage.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
  60. ctx.setFillColor(CGColor(red: red.pointee, green: green.pointee, blue: blue.pointee, alpha: alpha.pointee))
  61. ctx.fill(area)
  62. ctx.restoreGState()
  63. ctx.setBlendMode(CGBlendMode.multiply)
  64. ctx.draw(baseImage.cgImage(forProposedRect: nil, context: nil, hints: nil)!, in: area)
  65. ctx.draw(frameImage.cgImage(forProposedRect: nil, context: nil, hints: nil)!, in: area)
  66. if let newImage = ctx.makeImage() {
  67. let image = NSImage(cgImage: newImage, size: size)
  68. return image
  69. } else {
  70. return NSImage()
  71. }
  72. } else {
  73. return NSImage()
  74. }
  75. }
  76. class func image(with size: NSSize, drawingHandler:((_ dstRect: NSRect) -> Bool)?) -> NSImage {
  77. var image = NSImage(size: size)
  78. image.lockFocus()
  79. if (drawingHandler != nil) {
  80. _ = drawingHandler!(NSMakeRect(0, 0, size.width, size.height))
  81. }
  82. image.unlockFocus()
  83. return image
  84. }
  85. func pngData() -> Data? {
  86. guard let data = self.tiffRepresentation else {
  87. return nil
  88. }
  89. let imageRep = NSBitmapImageRep(data: data)
  90. imageRep?.size = self.size
  91. return imageRep?.representation(using: .png, properties: [:])
  92. }
  93. func jpgData() -> Data? {
  94. guard let data = self.tiffRepresentation else {
  95. return nil
  96. }
  97. let imageRep = NSBitmapImageRep(data: data)
  98. imageRep?.size = self.size
  99. return imageRep?.representation(using: .jpeg, properties: [:])
  100. }
  101. class func isDamageImage(_ image: NSImage?, imagePath path: String) -> Bool {
  102. let addImageAnnotation = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last! as NSString).appendingPathComponent(Bundle.main.bundleIdentifier!)
  103. if !FileManager.default.fileExists(atPath: addImageAnnotation) {
  104. try? FileManager.default.createDirectory(atPath: addImageAnnotation, withIntermediateDirectories: false, attributes: nil)
  105. }
  106. let data = image?.tiffRepresentation
  107. let imageRep = NSBitmapImageRep(data: data!)
  108. imageRep?.size = image?.size ?? NSSize.zero
  109. var imageData: Data?
  110. if path.lowercased() == "png" {
  111. imageData = imageRep?.representation(using: .png, properties: [:])
  112. } else {
  113. imageData = imageRep?.representation(using: .jpeg, properties: [:])
  114. }
  115. let rPath = (addImageAnnotation as NSString).appendingPathComponent("waterAnnotation.png")
  116. if ((try? imageData?.write(to: URL(fileURLWithPath: rPath), options: .atomic)) == nil) {
  117. return true
  118. } else {
  119. return false
  120. }
  121. }
  122. }