RCTImageUtils.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #import <UIKit/UIKit.h>
  8. #import <React/RCTDefines.h>
  9. #import <React/RCTResizeMode.h>
  10. NS_ASSUME_NONNULL_BEGIN
  11. /**
  12. * This function takes an source size (typically from an image), a target size
  13. * and scale that it will be drawn at (typically in a CGContext) and then
  14. * calculates the rectangle to draw the image into so that it will be sized and
  15. * positioned correctly according to the specified resizeMode.
  16. */
  17. RCT_EXTERN CGRect RCTTargetRect(CGSize sourceSize, CGSize destSize,
  18. CGFloat destScale, RCTResizeMode resizeMode);
  19. /**
  20. * This function takes a source size (typically from an image), a target rect
  21. * that it will be drawn into (typically relative to a CGContext), and works out
  22. * the transform needed to draw the image at the correct scale and position.
  23. */
  24. RCT_EXTERN CGAffineTransform RCTTransformFromTargetRect(CGSize sourceSize,
  25. CGRect targetRect);
  26. /**
  27. * This function takes an input content size & scale (typically from an image),
  28. * a target size & scale at which it will be displayed (typically in a
  29. * UIImageView) and then calculates the optimal size at which to redraw the
  30. * image so that it will be displayed correctly with the specified resizeMode.
  31. */
  32. RCT_EXTERN CGSize RCTTargetSize(CGSize sourceSize, CGFloat sourceScale,
  33. CGSize destSize, CGFloat destScale,
  34. RCTResizeMode resizeMode, BOOL allowUpscaling);
  35. /**
  36. * This function takes an input content size & scale (typically from an image),
  37. * a target size & scale that it will be displayed at, and determines if the
  38. * source will need to be upscaled to fit (which may result in pixelization).
  39. */
  40. RCT_EXTERN BOOL RCTUpscalingRequired(CGSize sourceSize, CGFloat sourceScale,
  41. CGSize destSize, CGFloat destScale,
  42. RCTResizeMode resizeMode);
  43. /**
  44. * This function takes the source data for an image and decodes it at the
  45. * specified size. If the original image is smaller than the destination size,
  46. * the resultant image's scale will be decreased to compensate, so the
  47. * width/height of the returned image is guaranteed to be >= destSize.
  48. * Pass a destSize of CGSizeZero to decode the image at its original size.
  49. */
  50. RCT_EXTERN UIImage *__nullable RCTDecodeImageWithData(NSData *data,
  51. CGSize destSize,
  52. CGFloat destScale,
  53. RCTResizeMode resizeMode);
  54. /**
  55. * This function takes the source data for an image and decodes just the
  56. * metadata, without decompressing the image itself.
  57. */
  58. RCT_EXTERN NSDictionary<NSString *, id> *__nullable RCTGetImageMetadata(NSData *data);
  59. /**
  60. * Convert an image back into data. Images with an alpha channel will be
  61. * converted to lossless PNG data. Images without alpha will be converted to
  62. * JPEG. The `quality` argument controls the compression ratio of the JPEG
  63. * conversion, with 1.0 being maximum quality. It has no effect for images
  64. * using PNG compression.
  65. */
  66. RCT_EXTERN NSData *__nullable RCTGetImageData(UIImage *image, float quality);
  67. /**
  68. * This function transforms an image. `destSize` is the size of the final image,
  69. * and `destScale` is its scale. The `transform` argument controls how the
  70. * source image will be mapped to the destination image.
  71. */
  72. RCT_EXTERN UIImage *__nullable RCTTransformImage(UIImage *image,
  73. CGSize destSize,
  74. CGFloat destScale,
  75. CGAffineTransform transform);
  76. /*
  77. * Return YES if image has an alpha component
  78. */
  79. RCT_EXTERN BOOL RCTImageHasAlpha(CGImageRef image);
  80. NS_ASSUME_NONNULL_END