2023-04-22 14:52:27 -07:00
|
|
|
import CoreImage
|
2023-04-22 15:43:22 -07:00
|
|
|
import Foundation
|
2023-04-22 14:52:27 -07:00
|
|
|
import UniformTypeIdentifiers
|
|
|
|
|
|
|
|
extension CGImage {
|
|
|
|
func toPngData() throws -> Data {
|
|
|
|
guard let data = CFDataCreateMutable(nil, 0) else {
|
2023-04-22 15:43:22 -07:00
|
|
|
throw SdCoreError.imageEncode
|
2023-04-22 14:52:27 -07:00
|
|
|
}
|
2023-04-22 15:43:22 -07:00
|
|
|
|
2023-04-22 14:52:27 -07:00
|
|
|
guard let destination = CGImageDestinationCreateWithData(data, "public.png" as CFString, 1, nil) else {
|
2023-04-22 15:43:22 -07:00
|
|
|
throw SdCoreError.imageEncode
|
2023-04-22 14:52:27 -07:00
|
|
|
}
|
2023-04-22 15:43:22 -07:00
|
|
|
|
2023-04-22 14:52:27 -07:00
|
|
|
CGImageDestinationAddImage(destination, self, nil)
|
|
|
|
if CGImageDestinationFinalize(destination) {
|
|
|
|
return data as Data
|
|
|
|
} else {
|
2023-04-22 15:43:22 -07:00
|
|
|
throw SdCoreError.imageEncode
|
2023-04-22 14:52:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|