9

我试图显示一个简单的NSImageView,它的图像居中而不像这样缩放它:

就像 iOS 在设置 UIView 的 contentMode = **UIViewContentModeCenter** 时所做的那样

就像 iOS 在设置 UIView 的 contentMode = UIViewContentModeCenter时所做的那样

所以我尝试了所有NSImageScaling值,这就是我选择NSScaleNone时得到的

我真的不明白发生了什么:-/

我真的不明白发生了什么:-/

4

4 回答 4

7

您可以手动生成正确大小和内容的图像,并将其设置为 NSImageView 的图像,以便 NSImageView 不需要做任何事情。

NSImage *newImg = [self resizeImage:sourceImage size:newSize];
[aNSImageView setImage:newImg];

以下函数调整图像大小以适应新大小,同时保持纵横比不变。如果图像小于新尺寸,则将其放大并用新框架填充。如果图像大于新尺寸,则缩小尺寸,并用新框架填充

- (NSImage*) resizeImage:(NSImage*)sourceImage size:(NSSize)size{

NSRect targetFrame = NSMakeRect(0, 0, size.width, size.height);
NSImage*  targetImage = [[NSImage alloc] initWithSize:size];

NSSize sourceSize = [sourceImage size];

float ratioH = size.height/ sourceSize.height;
float ratioW = size.width / sourceSize.width;

NSRect cropRect = NSZeroRect;
if (ratioH >= ratioW) {
    cropRect.size.width = floor (size.width / ratioH);
    cropRect.size.height = sourceSize.height;
} else {
    cropRect.size.width = sourceSize.width;
    cropRect.size.height = floor(size.height / ratioW);
}

cropRect.origin.x = floor( (sourceSize.width - cropRect.size.width)/2 );
cropRect.origin.y = floor( (sourceSize.height - cropRect.size.height)/2 );



[targetImage lockFocus];

[sourceImage drawInRect:targetFrame
               fromRect:cropRect       //portion of source image to draw
              operation:NSCompositeCopy  //compositing operation
               fraction:1.0              //alpha (transparency) value
         respectFlipped:YES              //coordinate system
                  hints:@{NSImageHintInterpolation:
 [NSNumber numberWithInt:NSImageInterpolationLow]}];

[targetImage unlockFocus];

return targetImage;}
于 2013-08-05T17:16:36.000 回答
4

这是 NSImage 的一个很棒的类别:NSImage+ContentMode

它允许像 iOS 中那样的内容模式,效果很好。

于 2015-12-22T13:28:03.850 回答
2

将图像缩放属性设置为 NSImageScaleAxesIndependently,它将缩放图像以填充矩形。这不会保留纵横比。

于 2016-09-13T07:32:17.393 回答
1

@Shagru 答案的 Swift 版本(没有提示)

func resizeImage(_ sourceImage:NSImage, size:CGSize) -> NSImage
    {
        let targetFrame = CGRect(origin: CGPoint.zero, size: size);
        let targetImage = NSImage.init(size: size)
        let sourceSize = sourceImage.size
        let ratioH = size.height / sourceSize.height;
        let ratioW = size.width / sourceSize.width;

        var cropRect = CGRect.zero;
        if (ratioH >= ratioW) {
            cropRect.size.width = floor (size.width / ratioH);
            cropRect.size.height = sourceSize.height;
        } else {
            cropRect.size.width = sourceSize.width;
            cropRect.size.height = floor(size.height / ratioW);
        }

        cropRect.origin.x = floor( (sourceSize.width - cropRect.size.width)/2 );
        cropRect.origin.y = floor( (sourceSize.height - cropRect.size.height)/2 );
        targetImage.lockFocus()
        sourceImage.draw(in: targetFrame, from: cropRect, operation: .copy, fraction: 1.0, respectFlipped: true, hints: nil )


        targetImage.unlockFocus()
        return targetImage;
    }
于 2020-04-25T06:14:31.207 回答