-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSImage+Util.swift
136 lines (110 loc) · 4.42 KB
/
NSImage+Util.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import Cocoa
import Foundation
extension NSImage {
func resize(w: CGFloat, h: CGFloat) -> NSImage {
let destSize = NSMakeSize(CGFloat(w), CGFloat(h))
let imageRepresentation = NSBitmapImageRep(
bitmapDataPlanes: nil,
pixelsWide: Int(destSize.width),
pixelsHigh: Int(destSize.height),
bitsPerSample: 8,
samplesPerPixel: 4,
hasAlpha: true,
isPlanar: false,
colorSpaceName: .calibratedRGB,
bytesPerRow: 0,
bitsPerPixel: 0
)
imageRepresentation?.size = destSize
NSGraphicsContext.saveGraphicsState()
if let aRep = imageRepresentation {
NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: aRep)
}
self.draw(
in: NSMakeRect(0, 0, destSize.width, destSize.height),
from: NSZeroRect,
operation: NSCompositingOperation.copy,
fraction: 1.0
)
NSGraphicsContext.restoreGraphicsState()
let newImage = NSImage(size: destSize)
if let aRep = imageRepresentation {
newImage.addRepresentation(aRep)
}
return newImage
}
func ciImage() -> CIImage? {
guard let data = self.tiffRepresentation,
let bitmap = NSBitmapImageRep(data: data) else {
return nil
}
let ci = CIImage(bitmapImageRep: bitmap)
return ci
}
func parseQR(QR: CIImage?) -> [String?] {
let image = QR
let detector = CIDetector(ofType: CIDetectorTypeQRCode,
context: nil,
options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])
let features = detector?.features(in: image!) ?? []
return features.compactMap { feature in
return (feature as? CIQRCodeFeature)?.messageString
}
}
func merge(with other: NSImage?) -> NSImage? {
guard let otherImage = other else {
return nil
}
let view = NSView(frame: NSRect(origin: .zero, size: size))
let backgroundImageView = NSImageView(frame: NSRect(origin: .zero, size: size))
backgroundImageView.image = self
let pixelImageView = NSImageView(frame: NSRect(origin: .zero, size: size))
pixelImageView.image = otherImage
view.addSubview(backgroundImageView)
view.addSubview(pixelImageView)
return view.toImage()
}
var cgImage: CGImage? {
var imageRect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
let imageRef = self.cgImage(forProposedRect: &imageRect, context: nil, hints: nil)
return imageRef
}
func maskWithGradient(start: NSColor, end: NSColor) -> NSImage? {
let width = self.size.width
let height = self.size.height
let bounds = NSRect(x: 0, y: 0, width: width, height: height)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
let context = CGContext(
data: nil,
width: Int(width),
height: Int(height),
bitsPerComponent: 8,
bytesPerRow: 0,
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: bitmapInfo.rawValue
)
guard let bitmapContext = context, let maskImage = self.cgImage else {
return nil
}
let locations: [CGFloat] = [0.0, 1.0]
let colors = [start.cgColor, end.cgColor] as CFArray
let startPoint = CGPoint(x: width / 2, y: 0)
let endPoint = CGPoint(x: width / 2, y: height)
bitmapContext.clip(to: bounds, mask: maskImage)
guard let gradient = CGGradient(colorsSpace: colorSpace, colors: colors, locations: locations) else {
return nil
}
bitmapContext.drawLinearGradient(
gradient,
start: startPoint,
end: endPoint,
options: CGGradientDrawingOptions(rawValue: UInt32(0))
)
if let cImage = bitmapContext.makeImage() {
let coloredImage = NSImage(cgImage: cImage, size: bounds.size)
return coloredImage
}
return nil
}
}