NSImage+Compatibility.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #if SD_MAC
  10. /**
  11. This category is provided to easily write cross-platform(AppKit/UIKit) code. For common usage, see `UIImage+Metadata.h`.
  12. */
  13. @interface NSImage (Compatibility)
  14. /**
  15. The underlying Core Graphics image object. This will actually use `CGImageForProposedRect` with the image size.
  16. */
  17. @property (nonatomic, readonly, nullable) CGImageRef CGImage;
  18. /**
  19. The underlying Core Image data. This will actually use `bestRepresentationForRect` with the image size to find the `NSCIImageRep`.
  20. */
  21. @property (nonatomic, readonly, nullable) CIImage *CIImage;
  22. /**
  23. The scale factor of the image. This wil actually use `bestRepresentationForRect` with image size and pixel size to calculate the scale factor. If failed, use the default value 1.0. Should be greater than or equal to 1.0.
  24. */
  25. @property (nonatomic, readonly) CGFloat scale;
  26. // These are convenience methods to make AppKit's `NSImage` match UIKit's `UIImage` behavior. The scale factor should be greater than or equal to 1.0.
  27. /**
  28. Returns an image object with the scale factor and orientation. The representation is created from the Core Graphics image object.
  29. @note The difference between this and `initWithCGImage:size` is that `initWithCGImage:size` will actually create a `NSCGImageSnapshotRep` representation and always use `backingScaleFactor` as scale factor. So we should avoid it and use `NSBitmapImageRep` with `initWithCGImage:` instead.
  30. @note The difference between this and UIKit's `UIImage` equivalent method is the way to process orientation. If the provided image orientation is not equal to Up orientation, this method will firstly rotate the CGImage to the correct orientation to work compatible with `NSImageView`. However, UIKit will not actually rotate CGImage and just store it as `imageOrientation` property.
  31. @param cgImage A Core Graphics image object
  32. @param scale The image scale factor
  33. @param orientation The orientation of the image data
  34. @return The image object
  35. */
  36. - (nonnull instancetype)initWithCGImage:(nonnull CGImageRef)cgImage scale:(CGFloat)scale orientation:(CGImagePropertyOrientation)orientation;
  37. /**
  38. Initializes and returns an image object with the specified Core Image object. The representation is `NSCIImageRep`.
  39. @param ciImage A Core Image image object
  40. @param scale The image scale factor
  41. @param orientation The orientation of the image data
  42. @return The image object
  43. */
  44. - (nonnull instancetype)initWithCIImage:(nonnull CIImage *)ciImage scale:(CGFloat)scale orientation:(CGImagePropertyOrientation)orientation;
  45. /**
  46. Returns an image object with the scale factor. The representation is created from the image data.
  47. @note The difference between these this and `initWithData:` is that `initWithData:` will always use `backingScaleFactor` as scale factor.
  48. @param data The image data
  49. @param scale The image scale factor
  50. @return The image object
  51. */
  52. - (nullable instancetype)initWithData:(nonnull NSData *)data scale:(CGFloat)scale;
  53. @end
  54. #endif