Files
stable-diffusion-rpc/Sources/StableDiffusionCore/ImageExtensions.swift

23 lines
623 B
Swift
Raw Normal View History

2023-04-22 14:52:27 -07:00
import CoreImage
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 {
throw SdCoreError.imageEncode
2023-04-22 14:52:27 -07:00
}
2023-04-22 14:52:27 -07:00
guard let destination = CGImageDestinationCreateWithData(data, "public.png" as CFString, 1, nil) else {
throw SdCoreError.imageEncode
2023-04-22 14:52:27 -07:00
}
2023-04-22 14:52:27 -07:00
CGImageDestinationAddImage(destination, self, nil)
if CGImageDestinationFinalize(destination) {
return data as Data
} else {
throw SdCoreError.imageEncode
2023-04-22 14:52:27 -07:00
}
}
}