nativeImageSource.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @flow strict-local
  8. * @format
  9. */
  10. 'use strict';
  11. import Platform from '../Utilities/Platform';
  12. import type {ImageURISource} from './ImageSource';
  13. type NativeImageSourceSpec = $ReadOnly<{|
  14. android?: string,
  15. ios?: string,
  16. default?: string,
  17. // For more details on width and height, see
  18. // https://reactnative.dev/docs/images.html#why-not-automatically-size-everything
  19. height: number,
  20. width: number,
  21. |}>;
  22. /**
  23. * In hybrid apps, use `nativeImageSource` to access images that are already
  24. * available on the native side, for example in Xcode Asset Catalogs or
  25. * Android's drawable folder.
  26. *
  27. * However, keep in mind that React Native Packager does not guarantee that the
  28. * image exists. If the image is missing you'll get an empty box. When adding
  29. * new images your app needs to be recompiled.
  30. *
  31. * Prefer Static Image Resources system which provides more guarantees,
  32. * automates measurements and allows adding new images without rebuilding the
  33. * native app. For more details visit:
  34. *
  35. * https://reactnative.dev/docs/images.html
  36. *
  37. */
  38. function nativeImageSource(spec: NativeImageSourceSpec): ImageURISource {
  39. let uri = Platform.select({
  40. android: spec.android,
  41. default: spec.default,
  42. ios: spec.ios,
  43. });
  44. if (uri == null) {
  45. console.warn(
  46. 'nativeImageSource(...): No image name supplied for `%s`:\n%s',
  47. Platform.OS,
  48. JSON.stringify(spec, null, 2),
  49. );
  50. uri = '';
  51. }
  52. return {
  53. deprecated: true,
  54. height: spec.height,
  55. uri,
  56. width: spec.width,
  57. };
  58. }
  59. module.exports = nativeImageSource;