Remove the extra category method for `NSBitmapImageRep`. Only use `NSImage`'s native API to manage it
This commit is contained in:
parent
cebf72d6d5
commit
e2c99f437d
|
@ -27,7 +27,7 @@ The underlying Core Graphics image object. This will actually use `CGImageForPro
|
|||
|
||||
/**
|
||||
Returns an image object with the scale factor and orientation. The representation is created from the Core Graphics image object.
|
||||
@note The difference between this and `initWithCGImage:size` is that `initWithCGImage:size` will create a `NSCGImageSnapshotRep` but not `NSBitmapImageRep` instance. And it will always use `backingScaleFactor` as scale factor.
|
||||
@note The difference between this and `initWithCGImage:size` is that `initWithCGImage:size` will use `backingScaleFactor` as scale factor if you specify `NSZeroSize` and does not support orientation.
|
||||
@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.
|
||||
|
||||
@param cgImage A Core Graphics image object
|
||||
|
@ -49,13 +49,4 @@ The underlying Core Graphics image object. This will actually use `CGImageForPro
|
|||
|
||||
@end
|
||||
|
||||
@interface NSBitmapImageRep (Additions)
|
||||
|
||||
// These methods' function is the same as `NSImage`'s function. For `NSBitmapImageRep`.
|
||||
|
||||
- (nonnull instancetype)initWithCGImage:(nonnull CGImageRef)cgImage scale:(CGFloat)scale orientation:(CGImagePropertyOrientation)orientation;
|
||||
- (nullable instancetype)initWithData:(nonnull NSData *)data scale:(CGFloat)scale;
|
||||
|
||||
@end
|
||||
|
||||
#endif
|
||||
|
|
|
@ -11,7 +11,6 @@
|
|||
#if SD_MAC
|
||||
|
||||
#import "SDWebImageCoderHelper.h"
|
||||
#import "objc/runtime.h"
|
||||
|
||||
@implementation NSImage (Additions)
|
||||
|
||||
|
@ -33,7 +32,7 @@
|
|||
CGFloat widthScale = pixelWidth / width;
|
||||
CGFloat heightScale = pixelHeight / height;
|
||||
if (widthScale == heightScale && widthScale >= 1) {
|
||||
// Protect for image object which custom the size.
|
||||
// Protect because there may be `NSImageRepMatchesDevice` (0)
|
||||
scale = widthScale;
|
||||
}
|
||||
}
|
||||
|
@ -41,71 +40,40 @@
|
|||
return scale;
|
||||
}
|
||||
|
||||
- (instancetype)initWithCGImage:(CGImageRef)cgImage scale:(CGFloat)scale {
|
||||
return [self initWithCGImage:cgImage scale:scale orientation:kCGImagePropertyOrientationUp];
|
||||
}
|
||||
|
||||
- (instancetype)initWithCGImage:(CGImageRef)cgImage scale:(CGFloat)scale orientation:(CGImagePropertyOrientation)orientation {
|
||||
if (scale < 1) {
|
||||
scale = 1;
|
||||
}
|
||||
NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage scale:scale orientation:orientation];
|
||||
NSSize size = imageRep.size;
|
||||
self = [self initWithSize:size];
|
||||
if (self) {
|
||||
[self addRepresentation:imageRep];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithData:(NSData *)data scale:(CGFloat)scale {
|
||||
if (scale < 1) {
|
||||
scale = 1;
|
||||
}
|
||||
NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithData:data scale:scale];
|
||||
if (!imageRep) {
|
||||
return nil;
|
||||
}
|
||||
NSSize size = imageRep.size;
|
||||
self = [self initWithSize:size];
|
||||
if (self) {
|
||||
[self addRepresentation:imageRep];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation NSBitmapImageRep (Additions)
|
||||
|
||||
- (instancetype)initWithCGImage:(CGImageRef)cgImage scale:(CGFloat)scale orientation:(CGImagePropertyOrientation)orientation {
|
||||
CGFloat pixelWidth = CGImageGetWidth(cgImage);
|
||||
CGFloat pixelHeight = CGImageGetHeight(cgImage);
|
||||
NSSize size = NSMakeSize(pixelWidth / scale, pixelHeight / scale);
|
||||
if (orientation != kCGImagePropertyOrientationUp) {
|
||||
// AppKit design is different from UIKit. Where CGImage based image rep does not respect to any orientation. Only data based image rep which contains the EXIF metadata can automatically detect orientation.
|
||||
// This should be nonnull, until the memory is exhausted cause `CGBitmapContextCreate` failed.
|
||||
cgImage = [SDWebImageCoderHelper CGImageCreateDecoded:cgImage orientation:orientation];
|
||||
self = [self initWithCGImage:cgImage];
|
||||
self = [self initWithCGImage:cgImage size:size];
|
||||
CGImageRelease(cgImage);
|
||||
} else {
|
||||
self = [self initWithCGImage:cgImage];
|
||||
}
|
||||
if (self) {
|
||||
if (scale < 1) {
|
||||
scale = 1;
|
||||
}
|
||||
NSSize size = NSMakeSize(self.pixelsWide / scale, self.pixelsHigh / scale);
|
||||
self.size = size;
|
||||
self = [self initWithCGImage:cgImage size:size];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithData:(NSData *)data scale:(CGFloat)scale {
|
||||
self = [self initWithData:data];
|
||||
NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithData:data];
|
||||
if (!imageRep) {
|
||||
return nil;
|
||||
}
|
||||
if (scale < 1) {
|
||||
scale = 1;
|
||||
}
|
||||
CGFloat pixelWidth = imageRep.pixelsWide;
|
||||
CGFloat pixelHeight = imageRep.pixelsHigh;
|
||||
NSSize size = NSMakeSize(pixelWidth / scale, pixelHeight / scale);
|
||||
self = [self initWithSize:size];
|
||||
if (self) {
|
||||
if (scale < 1) {
|
||||
scale = 1;
|
||||
}
|
||||
NSSize size = NSMakeSize(self.pixelsWide / scale, self.pixelsHigh / scale);
|
||||
self.size = size;
|
||||
imageRep.size = size;
|
||||
[self addRepresentation:imageRep];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue