SDGraphicsImageRenderer.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import "SDWebImageCompat.h"
  9. /**
  10. These following class are provided to use `UIGraphicsImageRenderer` with polyfill, which allows write cross-platform(AppKit/UIKit) code and avoid runtime version check.
  11. Compared to `UIGraphicsBeginImageContext`, `UIGraphicsImageRenderer` use dynamic bitmap from your draw code to generate CGContext, not always use ARGB8888, which is more performant on RAM usage.
  12. Which means, if you draw CGImage/CIImage which contains grayscale only, the underlaying bitmap context use grayscale, it's managed by system and not a fixed type. (actually, the `kCGContextTypeAutomatic`)
  13. For usage, See more in Apple's documentation: https://developer.apple.com/documentation/uikit/uigraphicsimagerenderer
  14. For UIKit on iOS/tvOS 10+, these method just use the same `UIGraphicsImageRenderer` API.
  15. For others (macOS/watchOS or iOS/tvOS 10-), these method use the `SDImageGraphics.h` to implements the same behavior (but without dynamic bitmap support)
  16. */
  17. typedef void (^SDGraphicsImageDrawingActions)(CGContextRef _Nonnull context);
  18. typedef NS_ENUM(NSInteger, SDGraphicsImageRendererFormatRange) {
  19. SDGraphicsImageRendererFormatRangeUnspecified = -1,
  20. SDGraphicsImageRendererFormatRangeAutomatic = 0,
  21. SDGraphicsImageRendererFormatRangeExtended,
  22. SDGraphicsImageRendererFormatRangeStandard
  23. };
  24. /// A set of drawing attributes that represent the configuration of an image renderer context.
  25. @interface SDGraphicsImageRendererFormat : NSObject
  26. /// The display scale of the image renderer context.
  27. /// The default value is equal to the scale of the main screen.
  28. @property (nonatomic) CGFloat scale;
  29. /// A Boolean value indicating whether the underlying Core Graphics context has an alpha channel.
  30. /// The default value is NO.
  31. @property (nonatomic) BOOL opaque;
  32. /// Specifying whether the bitmap context should use extended color.
  33. /// For iOS 12+, the value is from system `preferredRange` property
  34. /// For iOS 10-11, the value is from system `prefersExtendedRange` property
  35. /// For iOS 9-, the value is `.standard`
  36. @property (nonatomic) SDGraphicsImageRendererFormatRange preferredRange;
  37. /// Init the default format. See each properties's default value.
  38. - (nonnull instancetype)init;
  39. /// Returns a new format best suited for the main screen’s current configuration.
  40. + (nonnull instancetype)preferredFormat;
  41. @end
  42. /// A graphics renderer for creating Core Graphics-backed images.
  43. @interface SDGraphicsImageRenderer : NSObject
  44. /// Creates an image renderer for drawing images of a given size.
  45. /// @param size The size of images output from the renderer, specified in points.
  46. /// @return An initialized image renderer.
  47. - (nonnull instancetype)initWithSize:(CGSize)size;
  48. /// Creates a new image renderer with a given size and format.
  49. /// @param size The size of images output from the renderer, specified in points.
  50. /// @param format A SDGraphicsImageRendererFormat object that encapsulates the format used to create the renderer context.
  51. /// @return An initialized image renderer.
  52. - (nonnull instancetype)initWithSize:(CGSize)size format:(nonnull SDGraphicsImageRendererFormat *)format;
  53. /// Creates an image by following a set of drawing instructions.
  54. /// @param actions A SDGraphicsImageDrawingActions block that, when invoked by the renderer, executes a set of drawing instructions to create the output image.
  55. /// @note You should not retain or use the context outside the block, it's non-escaping.
  56. /// @return A UIImage object created by the supplied drawing actions.
  57. - (nonnull UIImage *)imageWithActions:(nonnull NS_NOESCAPE SDGraphicsImageDrawingActions)actions;
  58. @end