0

我实现了一个导航到相机活动的本机模块。但是从 nativeModules 调用相机时,打开需要 1500-2000ms(不包括相机中的 ui 更新)。

扩展的原生模块类ReactContextBaseJavaModule是这样写的:

@ReactModule(name = ImagePickerModule.NAME)
public class ImagePickerModule extends ReactContextBaseJavaModule
        implements ActivityEventListener
{
   ....
   // Calling this method
   @ReactMethod
   public void launchCamera(final ReadableMap options, final Callback callback) {
      // this method opens camera after checking necessary permissions and applies UI changes which was set by the user previously
   }
   ....
}

这是.ts文件中的 nativeModules 到 JS 的映射

import {NativeModules} from 'react-native';
import {ImagePickerNativeModule} from './privateTypes';

const NativeInterface: ImagePickerNativeModule | undefined =
  NativeModules.ImagePickerManager;

const DEFAULT_OPTIONS: ImagePickerOptions = {
   // options here
}

class ImagePicker {
  launchCamera(options: ImagePickerOptions, callback: Callback): void {
    return NativeInterface.launchCamera(
      {
        ...DEFAULT_OPTIONS,
        ...options,
        tintColor: processColor(options.tintColor || DEFAULT_OPTIONS.tintColor),
      },
      callback,
    );
  }
}

我正在从 react-native 代码中调用 nativeModule,如下所示:

export const CustomCamera = (): Promise<{ uri: string }> => {
    return new Promise((resolve, reject) => {
        ImagePicker.launchCamera(***Camera options passed***)
          .then(res => // Do something here)
          .catch(err => // Do something here)
    }
}

有什么方法可以在从本机模块调用活动时更快地打开活动?或者将活动保持在后台,以便在从 react-native 调用它时可以快速加载cameraActivity?请建议。

4

0 回答 0