Skip to content

Commit

Permalink
namespaced extension (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahsmartin authored Aug 8, 2024
1 parent ce7be2f commit fa4f1eb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// NSImage+PNG.swift
// NSImage+EMG.swift
//
//
// Created by Noah Martin on 8/2/24.
Expand All @@ -8,15 +8,27 @@
#if canImport(AppKit) && !targetEnvironment(macCatalyst)
import AppKit

extension NSImage {
public extension NSImage {
var emg: NSImageSnapshotsNamespace {
.init(image: self)
}

struct NSImageSnapshotsNamespace {
private let image: NSImage

init(image: NSImage) {
self.image = image
}

public func pngData() -> NSData? {
guard let tiffData = self.tiffRepresentation,
guard let tiffData = image.tiffRepresentation,
let bitmapImageRep = NSBitmapImageRep(data: tiffData) else {
return nil
}

let pngData = bitmapImageRep.representation(using: .png, properties: [:])
return pngData as NSData?
}
}
}
#endif
28 changes: 28 additions & 0 deletions Sources/SnapshotPreviewsCore/UIImage+EMG.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// UIImage+EMG.swift
//
//
// Created by Noah Martin on 8/8/24.
//

#if canImport(UIKit)
import UIKit

public extension UIImage {
var emg: UIImageSnapshotsNamespace {
.init(image: self)
}

struct UIImageSnapshotsNamespace {
private let image: UIImage

init(image: UIImage) {
self.image = image
}

public func pngData() -> Data? {
image.pngData()
}
}
}
#endif
2 changes: 1 addition & 1 deletion Sources/SnapshottingSwift/Snapshots.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Snapshots {
let file = Self.resultsDir.appendingPathComponent(fileName, isDirectory: false)
do {
let image = try imageResult.get()
if let pngData = image.pngData() {
if let pngData = image.emg.pngData() {
try pngData.write(to: file)
result["imagePath"] = file.path
} else {
Expand Down

0 comments on commit fa4f1eb

Please sign in to comment.