-
Notifications
You must be signed in to change notification settings - Fork 27
/
NSImage+String.swift
118 lines (83 loc) · 3.57 KB
/
NSImage+String.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
// UIImage+String.swift
// XCreds
//
// Created by Timothy Perfitt on 2/23/23.
//
import AppKit
extension NSImage {
static func imageFromPathOrURL(pathURLString:String) -> NSImage? {
var isFileURL = false
//if a local path, remove prefix and make a path URL out of it.
var pathURL:URL?
if pathURLString.hasPrefix("file://") == true {
isFileURL = true
let pathOnly = pathURLString.dropFirst(7)
pathURL = URL(fileURLWithPath: String(pathOnly))
}
else {
//otherwise it is a https
pathURL = URL(string: pathURLString)
}
//if we have a URL, try and load it
if let pathURL = pathURL {
TCSLogWithMark("PathURL: \(pathURL)")
//create cache folder if needed
let applicationSupportPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .localDomainMask, true)
let cacheDir = applicationSupportPath[0] as NSString
TCSLogWithMark("cachedir: \(cacheDir)")
let imageName = pathURL.lastPathComponent
let cacheFolder = cacheDir.appendingPathComponent("com.twocanoes.xcreds") as NSString
let imageFullPath = cacheFolder.appendingPathComponent(imageName) as NSString
TCSLogWithMark("imageFullPath: \(imageFullPath)")
if FileManager.default.fileExists(atPath: cacheFolder as String) == false {
TCSLogWithMark("cache folder doesn't exist, creating")
try? FileManager.default.createDirectory(atPath: cacheFolder as String, withIntermediateDirectories: true)
}
TCSLogWithMark("loading image")
//load image from URL
let image = NSImage.init(contentsOf: pathURL)
if pathURL.isFileURL==true{
TCSLogWithMark("path URL is a file URL, returning what we have")
return image
}
else {
TCSLogWithMark("path URL is a not file URL")
}
//if a valid image, then use that and cache it.
if let image = image {
TCSLogWithMark("image is valid")
let tiff = image.tiffRepresentation
if let tiff = tiff {
TCSLogWithMark("created TIF")
let url = URL(fileURLWithPath:imageFullPath as String)
if FileManager.default.fileExists(atPath: imageFullPath as String) == true {
TCSLogWithMark("\(imageFullPath) exists")
do {
TCSLogWithMark("removing \(imageFullPath)")
try FileManager.default.removeItem(atPath: imageFullPath as String)
}
catch{
TCSLogWithMark("error: \(error)")
}
}
TCSLogWithMark("writing out \(url)")
try? tiff.write(to:url )
}
TCSLogWithMark("returning image")
return image
}
else {
TCSLogWithMark("image is invalid")
}
TCSLogWithMark("getting cached image")
//if we couldn't get the nsurl image, use the cached one
let cachedImage = NSImage.init(contentsOfFile: imageFullPath as String)
if let cachedImage = cachedImage {
TCSLogWithMark("found image...returning")
return cachedImage
}
}
return nil
}
}