我试图在光标“翻转”期间在NSTableView
单元格中显示“信息”图标。我得到了单元格图像的副本,在其上绘制'info'图标,并告诉单元格使用此副本的setImage。只是绘制图标会缩放它,并且每张图片的大小不一样,因为表格中的图片大小不同。我没有遇到缩放或定位正确大小图标的问题。在tableView中重置图像imageCell模糊
我的问题是,置换后的图像稍微模糊,它的边缘仔细观察不脆。当mouseEntered发生并且图像被替换时,缺少边缘使得它看起来“稍微移动”。
我已经尝试了许多绘图技术,doen't上NSImage
(在CGBitmapContext
拉丝,或使用CIFilter
合成)使用lockFocus
,并且它们产生相同的结果。
我使用NSTableView
的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;
}
[这是斯科特·史蒂文森封闭的缩放方法]
- (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,所以不能用常规的新狮子的方法。
感谢您的参与......
谢谢,罗布。奇怪的是,这适用于缩放以适合单元格高度的图像,但对于比高度更小但填满宽度的缩放图像,“模糊”仍然存在。我正在回答这个问题,因为我认为它指出了另一个问题,这可能与查看原点坐标和缩放比例有关,这将是一个不同的问题。 – lulu