-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close #29; Fix compatibility with macOS with compile flags for UIImag…
…e and NSImage properties.
- Loading branch information
1 parent
fe434cb
commit 51f9c17
Showing
10 changed files
with
166 additions
and
7 deletions.
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// | ||
// NSImageExtension.swift | ||
// OpenAIKit | ||
// | ||
// Copyright (c) 2022 MarcoDotIO | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
// | ||
|
||
#if os(macOS) | ||
import Cocoa | ||
|
||
extension NSImage { | ||
public func pngData( | ||
size: ImageResolutions, | ||
imageInterpolation: NSImageInterpolation = .high | ||
) -> Data? { | ||
var cgSize = CGSize() | ||
|
||
switch size { | ||
case .small: | ||
cgSize.width = 256 | ||
cgSize.height = 256 | ||
break | ||
case .medium: | ||
cgSize.width = 512 | ||
cgSize.height = 512 | ||
break | ||
case .large: | ||
cgSize.width = 1024 | ||
cgSize.height = 1024 | ||
break | ||
} | ||
|
||
guard let bitmap = NSBitmapImageRep( | ||
bitmapDataPlanes: nil, | ||
pixelsWide: Int(cgSize.width), | ||
pixelsHigh: Int(cgSize.height), | ||
bitsPerSample: 8, | ||
samplesPerPixel: 4, | ||
hasAlpha: true, | ||
isPlanar: false, | ||
colorSpaceName: .deviceRGB, | ||
bitmapFormat: [], | ||
bytesPerRow: 0, | ||
bitsPerPixel: 0 | ||
) else { | ||
return nil | ||
} | ||
|
||
bitmap.size = cgSize | ||
NSGraphicsContext.saveGraphicsState() | ||
NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: bitmap) | ||
NSGraphicsContext.current?.imageInterpolation = imageInterpolation | ||
draw( | ||
in: NSRect(origin: .zero, size: cgSize), | ||
from: .zero, | ||
operation: .copy, | ||
fraction: 1.0 | ||
) | ||
NSGraphicsContext.restoreGraphicsState() | ||
|
||
return bitmap.representation(using: .png, properties: [:]) | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters