我试图在NSTableView
单元格上的光标“翻转”期间显示“信息”图标。我正在获取单元格图像的副本,在其上绘制“信息”图标,并告诉单元格使用此副本设置图像。只需绘制图标即可对其进行缩放,并且每个图标的尺寸都不相同,因为表格中的图像尺寸不同。我对缩放或定位正确大小的图标没有问题。
我的问题是替换的图像稍微模糊,仔细检查时边缘不清晰。当 mouseEntered 发生并且图像被替换时,缺少边缘使其看起来略微“移动”。
我尝试了许多lockFocus
在NSImage
(绘图CGBitmapContext
或使用CIFilter
合成)上不使用的绘图技术,它们产生了相同的结果。
我正在使用NSTableView
's preparedCellAtColumn
,因为似乎绘图willDisplayCell
是不可预测的——我在某个地方读过,但不记得在哪里。
这是我的preparedCellAtColumn
方法:
- (NSCell *)preparedCellAtColumn:(NSInteger)column row:(NSInteger)row
{
NSCell *cell = [super preparedCellAtColumn:column row:row];
if ((self.mouseOverRow == row) && column == 0) {
NSCell * imageCell = [super preparedCellAtColumn:0 row:row];
NSImage *sourceImage = [[imageCell image] copy];
NSRect cellRect = [self frameOfCellAtColumn:0 row:row];
NSSize cellSize = cellRect.size;
NSSize scaledSize = [sourceImage proportionalSizeForTargetSize:cellSize];
NSImage *outputImage = [[NSImage alloc] initWithSize:cellSize];
[outputImage setBackgroundColor:[NSColor clearColor]];
NSPoint drawPoint = NSZeroPoint;
drawPoint.x = (cellSize.width - scaledSize.width) * 0.5;
drawPoint.y = (cellSize.height - scaledSize.height) * 0.5;
NSRect drawRect = NSMakeRect(drawPoint.x, drawPoint.y, scaledSize.width, scaledSize.height);
NSPoint infoPoint = drawPoint;
infoPoint.x += NSWidth(drawRect) - self.infoSize.width;
[outputImage lockFocus];
[sourceImage drawInRect:drawRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[self.infoImage drawAtPoint:infoPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[outputImage unlockFocus];
[cell setImage:outputImage];
}
return cell;
}
[这是来自 scott stevenson 的封闭缩放方法]
- (NSSize)proportionalSizeForTargetSize:(NSSize)targetSize
{
NSSize imageSize = [self size];
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
if ( NSEqualSizes( imageSize, targetSize ) == NO )
{
CGFloat widthFactor;
CGFloat heightFactor;
widthFactor = targetWidth / width;
heightFactor = targetHeight / height;
if ( widthFactor < heightFactor )
scaleFactor = widthFactor;
else
scaleFactor = heightFactor;
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
}
return NSMakeSize(scaledWidth,scaledHeight);
}
我需要支持 10.6,所以不能使用新的 groovy lion 方法。
感谢您的考虑...