我使用下面的代码显示图像
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的大小
如何获得NSImage的实际显示尺寸?
欢迎任何评论
答案 0 :(得分:6)
我有这样的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);
}
答案 1 :(得分:0)
您可以在NSImage上使用size属性来获取显示大小吗?