gpt4 book ai didi

cocoa - 获取 NSImageView 上实际显示图像的大小

转载 作者:行者123 更新时间:2023-12-03 16:08:48 24 4
gpt4 key购买 nike

我使用下面的代码来显示图像

NSImage *image = [[[NSImage alloc] initWithContentsOfURL:url] autorelease];
NSImageView *newImageView = nil;
newImageView = [[NSImageView alloc] initWithFrame:[self bounds]];
[newImageView setImage:image];
[newImageView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];

NSImageView 会自动调整 NSimage 的大小

enter image description here

如何获取NSImage的实际显示尺寸?

欢迎大家留言

最佳答案

我有一个适用于iOS的方法,你也许可以适应它:

@implementation UIImageView (additions)
- (CGSize)imageScale {
CGFloat sx = self.frame.size.width / self.image.size.width;
CGFloat sy = self.frame.size.height / self.image.size.height;
CGFloat s = 1.0;
switch (self.contentMode) {
case UIViewContentModeScaleAspectFit:
s = fminf(sx, sy);
return CGSizeMake(s, s);
break;

case UIViewContentModeScaleAspectFill:
s = fmaxf(sx, sy);
return CGSizeMake(s, s);
break;

case UIViewContentModeScaleToFill:
return CGSizeMake(sx, sy);

default:
return CGSizeMake(s, s);
}
}

- (CGRect)imageRect {
CGSize imgScale = [self imageScale];
return CGRectCenteredInCGRect(CGRectFromCGSize(CGSizeScale(self.image.size, imgScale.width, imgScale.height)), self.frame);
}
@end



CGRect CGRectCenteredInCGRect(CGRect inner, CGRect outer) {
return CGRectMake((outer.size.width - inner.size.width) / 2.0, (outer.size.height - inner.size.height) / 2.0, inner.size.width, inner.size.height);
}

关于cocoa - 获取 NSImageView 上实际显示图像的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7929761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com