调整大小NSImage中定制NSTextFieldCell
问题描述:
我创造了阻力和使用苹果的例子下降应用:https://developer.apple.com/library/mac/samplecode/SourceView/Introduction/Intro.html调整大小NSImage中定制NSTextFieldCell
我拖着加载文件的实际图像。
当我拖累例如该图像文件到我NSOutlineView,我看到它以这种方式调整:
我作为是没有任何修改苹果的ImageAndTextCell类自定义NSTextFieldCell。
我如何调整此图像以适合比例单元矩形?
答
完美的作品。
@implementation NSImage (ProportionalScaling)
- (NSImage*)imageByScalingProportionallyToSize:(NSSize)targetSize
{
NSImage* sourceImage = self;
NSImage* newImage = nil;
if ([sourceImage isValid])
{
NSSize imageSize = [sourceImage size];
float width = imageSize.width;
float height = imageSize.height;
float targetWidth = targetSize.width;
float targetHeight = targetSize.height;
float scaleFactor = 0.0;
float scaledWidth = targetWidth;
float scaledHeight = targetHeight;
NSPoint thumbnailPoint = NSZeroPoint;
if (NSEqualSizes(imageSize, targetSize) == NO)
{
float widthFactor = targetWidth/width;
float heightFactor = targetHeight/height;
if (widthFactor < heightFactor)
scaleFactor = widthFactor;
else
scaleFactor = heightFactor;
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
if (widthFactor < heightFactor)
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
else if (widthFactor > heightFactor)
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
newImage = [[NSImage alloc] initWithSize:targetSize];
[newImage lockFocus];
NSRect thumbnailRect;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect: thumbnailRect
fromRect: NSZeroRect
operation: NSCompositeSourceOver
fraction: 1.0];
[newImage unlockFocus];
}
return [newImage autorelease];
}
@end
请检查此链接:http://theocacao.com/document.page/498 –
我试了一下,但是当我修改方法 - (无效)drawWithFrame:(的NSRect)cellFrame inView:(*的NSView)在ImageAndTextCell.m类中的controlView到NSImage * resizedImage = [self.myImage imageByScalingProportionallyToSize:....我有相同的结果,没有任何更改.. – KAMIKAZE
基于单元格的表已弃用。我建议你制作基于表格的视图,然后你可以在每行/列中绘制任何你喜欢的东西。 –