forked from huggingface/swift-coreml-diffusers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffusionImage+iOS.swift
63 lines (52 loc) · 1.97 KB
/
DiffusionImage+iOS.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// DiffusionImage+iOS.swift
// Diffusion
//
// Created by Dolmere and Pedro Cuenca on 30/07/2023.
//
import UIKit
import SwiftUI
import UniformTypeIdentifiers
extension DiffusionImage {
/// Instance func to place the generated image on the file system and return the `fileURL` where it is stored.
func save(cgImage: CGImage, filename: String?) -> URL? {
let image = UIImage(cgImage: cgImage)
let fn = filename ?? "diffusion_generated_image"
let appSupportURL = Settings.shared.tempStorageURL()
let fileURL = appSupportURL
.appendingPathComponent(fn)
.appendingPathExtension("png")
if let imageData = image.pngData() {
do {
try imageData.write(to: fileURL)
return fileURL
} catch {
print("Error saving image to temporary file: \(error)")
}
}
return nil
}
/// Returns a `Data` representation of this generated image in PNG format or nil if there is an error converting the image data.
func pngRepresentation() -> Data? {
let bitmapRep = UIImage(cgImage: cgImage).pngData()
return bitmapRep
}
}
extension DiffusionImage {
// MARK: - UIPasteboardWriting
func writableTypeIdentifiers(for pasteboard: UIPasteboard) -> [String] {
return [UTType.png.identifier]
}
func itemProviders(forActivityType activityType: UIActivity.ActivityType?) -> [NSItemProvider] {
let itemProvider = NSItemProvider()
itemProvider.registerDataRepresentation(forTypeIdentifier: UTType.png.identifier, visibility: .all) { completion in
guard let pngData = self.pngRepresentation() else {
completion(nil, NSError(domain: "DiffusionImageErrorDomain", code: 0, userInfo: nil))
return nil
}
completion(pngData, nil)
return nil
}
return [itemProvider]
}
}