Skip to content

Commit af1e911

Browse files
committed
[feat]: checking subdata
1 parent 9faeb4a commit af1e911

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Sources/ScriptToolkit/NSImageExtension.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,20 @@ public extension NSImage {
6363
///
6464
/// - parameter url: The location url to which to write the png file.
6565
func savePNGRepresentationToURL(url: URL, onlyChange: Bool = true) throws {
66-
if let png = self.PNGRepresentation {
66+
if let pngData = self.PNGRepresentation {
6767
if let originalData = try? Data(contentsOf: url) {
68-
if onlyChange && (png == originalData) {
68+
69+
// The beginning of PNG contains some data that may differ - a header
70+
let originalSubdata = originalData.subdata(in: 175 ..< originalData.endIndex)
71+
let pngSubdata = pngData.subdata(in: 175 ..< pngData.endIndex)
72+
73+
74+
if onlyChange && (pngSubdata == originalSubdata) {
6975
return
7076
}
7177
}
7278

73-
try png.write(to: url, options: .atomicWrite)
79+
try pngData.write(to: url, options: .atomicWrite)
7480
}
7581
}
7682

0 commit comments

Comments
 (0)