-
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
330 additions
and
224 deletions.
There are no files selected for viewing
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
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,38 @@ | ||
// | ||
// NSImage+Extension.swift | ||
// uPic | ||
// | ||
// Created by Svend Jin on 2019/12/12. | ||
// Copyright © 2019 Svend Jin. All rights reserved. | ||
// | ||
|
||
import Cocoa | ||
|
||
extension NSImage { | ||
var pngData: Data? { | ||
guard let rep = NSBitmapImageRep( | ||
bitmapDataPlanes: nil, | ||
pixelsWide: Int(size.width), | ||
pixelsHigh: Int(size.height), | ||
bitsPerSample: 8, | ||
samplesPerPixel: 4, | ||
hasAlpha: true, | ||
isPlanar: false, | ||
colorSpaceName: NSColorSpaceName.deviceRGB, | ||
bytesPerRow: 0, | ||
bitsPerPixel: 0 | ||
) else { | ||
print("Couldn't create bitmap representation") | ||
return nil | ||
} | ||
NSGraphicsContext.saveGraphicsState() | ||
NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: rep) | ||
draw(at: NSZeroPoint, from: NSZeroRect, operation: .sourceOver, fraction: 1.0) | ||
NSGraphicsContext.restoreGraphicsState() | ||
guard let data = rep.representation(using: NSBitmapImageRep.FileType.png, properties: [NSBitmapImageRep.PropertyKey.compressionFactor: 1.0]) else { | ||
print("Couldn't create PNG") | ||
return nil | ||
} | ||
return data | ||
} | ||
} |
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
Oops, something went wrong.