-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApp.swift
More file actions
53 lines (47 loc) · 1.49 KB
/
App.swift
File metadata and controls
53 lines (47 loc) · 1.49 KB
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
import SwiftUI
import blurhash
@main
struct SampleApp : App {
@UIApplicationDelegateAdaptor(SampleDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
class SampleDelegate: NSObject, UIApplicationDelegate {
func applicationDidReceiveMemoryWarning(_ application: UIApplication) {
BlurHash.shared.clearCache()
}
}
struct ContentView: View {
var body: some View {
VStack {
// Sample image.
let image = UIImage(named: "blueberries")!
Image(uiImage: image)
// Blur hashing.
let blurHash = BlurHash.shared.encode(
uiImage: image,
componentX: 5,
componentY: 4
)
Text(blurHash ?? "Invalid image")
.padding()
// Create blurred version.
// We don't need to create an UIImage in its full size.
// Let iOS scale it up for us as scaling is cheaper than generating a larger image.
if let blurHash = blurHash, let blurred = BlurHash.shared.decode(
blurHash: blurHash,
width: image.size.width / 4,
height: image.size.height / 4,
punch: 1.0,
useCache: true
) {
Image(uiImage: blurred)
.scaleEffect(4)
.frame(width: image.size.width, height: image.size.height)
}
}
}
}