示例

1import { Button, List, Navigation, NavigationStack, Script, Section, Text, } from "scripting"
2
3function Example() {
4  const dismiss = Navigation.useDismiss()
5
6  return <NavigationStack>
7    <List
8      navigationTitle={"Photos"}
9      navigationBarTitleDisplayMode={"inline"}
10      toolbar={{
11        cancellationAction: <Button
12          title={"Done"}
13          action={dismiss}
14        />
15      }}
16    >
17      <Section
18        footer={
19          <Text>Get the latest specified number of photos from the Photos app.</Text>
20        }
21      >
22        <Button
23          title={"Photos.getLatestPhotos"}
24          action={async () => {
25            const images = await Photos.getLatestPhotos(1)
26            const image = images?.[0]
27
28            if (image != null) {
29              Dialog.alert({
30                message: `Image size: ${image.width}*${image.height}`
31              })
32            } else {
33              Dialog.alert({
34                message: "Cancelled"
35              })
36            }
37          }}
38        />
39      </Section>
40
41      <Section
42        footer={
43          <Text>Present a photo picker dialog and pick limited number of photos.</Text>
44        }
45      >
46
47        <Button
48          title={"Photos.pickPhotos"}
49          action={async () => {
50            const images = await Photos.pickPhotos(1)
51            const image = images?.[0]
52
53            if (image != null) {
54              Dialog.alert({
55                message: `Image size: ${image.width}*${image.height}`
56              })
57            } else {
58              Dialog.alert({
59                message: "Cancelled"
60              })
61            }
62          }}
63        />
64      </Section>
65
66      <Section
67        footer={
68          <Text>Take a photo and return a UIImage instance.</Text>
69        }
70      >
71        <Button
72          title={"Photos.takePhoto"}
73          action={async () => {
74            const image = await Photos.takePhoto()
75
76            if (image != null) {
77              Dialog.alert({
78                message: `Image size: ${image.width}*${image.height}`
79              })
80            } else {
81              Dialog.alert({
82                message: "Cancelled"
83              })
84            }
85          }}
86        />
87      </Section>
88
89      <Section
90        footer={
91          <Text>Save an image to the Photos app. Returns a boolean value indicates that whether the operation is successful.</Text>
92        }
93      >
94        <Button
95          title={"Photos.savePhoto"}
96          action={async () => {
97            const image = await Photos.takePhoto()
98
99            if (image != null) {
100              const success = await Photos.savePhoto(Data.fromJPEG(image, 0.5)!)
101              Dialog.alert({
102                message: "The photo has been saved: " + success
103              })
104            } else {
105              Dialog.alert({
106                message: "Canceled"
107              })
108            }
109          }}
110        />
111      </Section>
112    </List>
113  </NavigationStack>
114}
115
116async function run() {
117  await Navigation.present({
118    element: <Example />
119  })
120
121  Script.exit()
122}
123
124run()