-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhotosPicker_Demo.swift
54 lines (43 loc) · 1.52 KB
/
PhotosPicker_Demo.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
//
// PhotosPicker_Demo.swift
// SwiftUI-Components-Library
//
// Created by msz on 2022/12/05.
//
import SwiftUI
import PhotosUI
struct PhotosPicker_Demo: View {
@State private var selectedImage: PhotosPickerItem?
@State private var newlyPickedImage: UIImage?
var body: some View {
Form {
PhotosPicker("Pick a new background image", selection: $selectedImage, matching: .images)
.onChange(of: selectedImage) { newValue in
if let newValue {
newValue.loadTransferable(type: Data.self) { result in
switch result {
case .success(let success):
if let success,
let imageObj = UIImage(data: success)
{
self.newlyPickedImage = imageObj
}
case .failure(let failure):
return
}
}
}
}
if let newlyPickedImage {
Image(uiImage: newlyPickedImage)
.resizable()
.scaledToFit()
}
}
}
}
struct PhotosPicker_Demo_Previews: PreviewProvider {
static var previews: some View {
PhotosPicker_Demo()
}
}