Fix the issue about scale factor using `initWithCGImage:size` macOS, we should always use `NSBitmapImageRep` to keep cross-platform compatible
This commit is contained in:
parent
d02c8d15e2
commit
5665b6fdf2
|
@ -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 use `backingScaleFactor` as scale factor if you specify `NSZeroSize` and does not support orientation.
|
||||
@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.
|
||||
@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
|
||||
|
|
|
@ -41,20 +41,26 @@
|
|||
}
|
||||
|
||||
- (instancetype)initWithCGImage:(CGImageRef)cgImage scale:(CGFloat)scale orientation:(CGImagePropertyOrientation)orientation {
|
||||
if (scale < 1) {
|
||||
scale = 1;
|
||||
}
|
||||
CGFloat pixelWidth = CGImageGetWidth(cgImage);
|
||||
CGFloat pixelHeight = CGImageGetHeight(cgImage);
|
||||
NSSize size = NSMakeSize(pixelWidth / scale, pixelHeight / scale);
|
||||
NSBitmapImageRep *imageRep;
|
||||
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 size:size];
|
||||
CGImageRelease(cgImage);
|
||||
CGImageRef rotatedCGImage = [SDWebImageCoderHelper CGImageCreateDecoded:cgImage orientation:orientation];
|
||||
imageRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
|
||||
CGImageRelease(rotatedCGImage);
|
||||
} else {
|
||||
self = [self initWithCGImage:cgImage size:size];
|
||||
imageRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
|
||||
}
|
||||
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) {
|
||||
imageRep.size = size;
|
||||
[self addRepresentation:imageRep];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue