• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { taskpool } from '@kit.ArkTS';
17import { loadPixelMap } from './pixelMapTest';
18import { BusinessError } from '@kit.BasicServicesKit';
19
20@Entry
21@Component
22struct Index {
23  @State message: string = 'Hello World';
24  @State pixelMap: PixelMap | undefined = undefined;
25
26  private loadImageFromThread(): void {
27    const resourceMgr = getContext(this).resourceManager;
28    // 此处‘startIcon.png’为media下复制到rawfile文件夹中,请开发者自行替换,否则imageSource创建失败会导致后续无法正常执行。
29    resourceMgr.getRawFd('startIcon.png').then(rawFileDescriptor => {
30      taskpool.execute(loadPixelMap, rawFileDescriptor).then(pixelMap => {
31        if (pixelMap) {
32          this.pixelMap = pixelMap as PixelMap;
33          console.log('Succeeded in creating pixelMap.');
34          // 主线程释放pixelMap。由于子线程返回pixelMap时已调用setTransferDetached,所以此处能够立即释放pixelMap。
35          this.pixelMap.release();
36        } else {
37          console.error('Failed to create pixelMap.');
38        }
39      }).catch((e: BusinessError) => {
40        console.error('taskpool execute loadPixelMap failed. Code: ' + e.code + ', message: ' + e.message);
41      });
42    });
43  }
44
45  build() {
46    RelativeContainer() {
47      Text(this.message)
48        .id('HelloWorld')
49        .fontSize(50)
50        .fontWeight(FontWeight.Bold)
51        .alignRules({
52          center: { anchor: '__container__', align: VerticalAlign.Center },
53          middle: { anchor: '__container__', align: HorizontalAlign.Center }
54        })
55        .onClick(() => {
56          this.loadImageFromThread();
57          this.message = 'success';
58        })
59    }
60    .height('100%')
61    .width('100%')
62  }
63}
64