Skip to content

Commit

Permalink
fix: the plugin should work only where we could generate image Apple …
Browse files Browse the repository at this point in the history
…platform at the moment
  • Loading branch information
mackoj committed Sep 11, 2024
1 parent 12fe639 commit 18a2ba7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#if canImport(SwiftUI)
import Foundation

#if !os(macOS)
#if canImport(UIKit)
import UIKit.UIImage
public typealias SnapImage = UIImage
#else
#elseif canImport(AppKit)
import AppKit.NSImage
public typealias SnapImage = NSImage
#endif
Expand Down Expand Up @@ -39,3 +40,4 @@ public protocol ImageSerializationPlugin {
func encodeImage(_ image: SnapImage) /*async throws*/ -> Data?
func decodeImage(_ data: Data) /*async throws*/ -> SnapImage?
}
#endif
13 changes: 7 additions & 6 deletions Sources/SnapshotTesting/Plug-ins/ImageSerializer.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#if canImport(SwiftUI)
import Foundation
import ImageSerializationPlugin

#if canImport(UIKit)
import UIKit
#endif
#if canImport(AppKit)
#elseif canImport(AppKit)
import AppKit
#endif

Expand Down Expand Up @@ -39,9 +39,9 @@ public class ImageSerializer {

// MARK: - Actual default Image Serializer
private func encodePNG(_ image: SnapImage) -> Data? {
#if !os(macOS)
#if canImport(UIKit)
return image.pngData()
#else
#elseif canImport(AppKit)
guard let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil) else {
return nil
}
Expand All @@ -51,10 +51,11 @@ public class ImageSerializer {
}

private func decodePNG(_ data: Data) -> SnapImage? {
#if !os(macOS)
#if canImport(UIKit)
return UIImage(data: data)
#else
#elseif canImport(AppKit)
return NSImage(data: data)
#endif
}
}
#endif
2 changes: 2 additions & 0 deletions Sources/SnapshotTesting/Plug-ins/PluginRegistry.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if canImport(SwiftUI)
import ImageSerializationPlugin

public class PluginRegistry {
Expand Down Expand Up @@ -44,3 +45,4 @@ func registerAllPlugins() {
classes.deallocate()
}
#endif
#endif

0 comments on commit 18a2ba7

Please sign in to comment.