我需要将图像从NSProgressIndicator放入NSOutlineView单元格中。我已经编写了用于确定指标的代码,并且效果很好:

NSProgressIndicator *progressIndicator = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect(0, 0, 16, 16)];
[progressIndicator setStyle:NSProgressIndicatorSpinningStyle];
[progressIndicator setIndeterminate:NO];
[progressIndicator setMaxValue:100.0];
[progressIndicator setDoubleValue:somePercentage];

NSImage *updateImage = [[NSImage alloc] initWithData:[progressIndicator dataWithPDFInsideRect:[progressIndicator frame]]];
[progressIndicator release];


return [updateImage autorelease];

我试图修改代码以也给我不确定的指标图像。但是对于不确定的情况,我总是得到空白的16x16图像。 (我已经通过在每种情况下将图像写入文件来确认了这一点,确定的情况给出了进度指示器图像,不确定的情况始终是16x16白色正方形)。

修改后的代码是:
if(self.lengthUnknown)
{
    NSProgressIndicator *progressIndicator = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect(0, 0, 16, 16)];
    [progressIndicator setStyle:NSProgressIndicatorSpinningStyle];
    [progressIndicator setIndeterminate:YES];

    NSImage *updateImage = [[NSImage alloc] initWithData:[progressIndicator dataWithPDFInsideRect:[progressIndicator frame]]];
    [progressIndicator release];


    return [updateImage autorelease];
}
else
{
 // Same code as the first listing, this case works fine
}

不确定的进度指示器是否使用某种导致-dataWithPDFInsideRect:无法捕获其图像的绘图?

更多信息:我尝试将进度指示器设置为不使用线程动画,并尝试通过NSImage的lockFocus方法获取内容,如下所示,但是这些尝试都没有任何效果。

Dave在下面提到的进度指示器单元格代码(AMIndeterminateProgressIndicatorCell)是一个很好的解决方法,但是我仍然想知道为什么我不能使用与确定模式相同的技术。

最佳答案

我已经使用AMIndeterminateProgressIndicatorCell来确定单元格中的不确定进度指示器。这不是一个真正的NSProgressIndicator,因为它是自己绘制的,但它是一个非常好的复制品IMO。

objective-c - 从NSProgressIndicator获取NSImage-LMLPHP
(来源:harmless.de)

10-05 19:57