objective-c - 从 NSProgressIndicator 获取 NSImage

标签 objective-c cocoa

我需要将 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 白色方 block )。

修改后的代码为:

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。

alt text
(来源:harmless.de)

关于objective-c - 从 NSProgressIndicator 获取 NSImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/262407/

相关文章:

objective-c - 系统偏好设置面板(安装)

objective-c - ArrayController 找不到 AppDelegate(Cocoa 中的绑定(bind))

iOS 将 subview 添加到具有动态高度的 ScrollView

objective-c - 如何从 View Controller 中 resignFirstResponder

iphone - NSTimeInterval -> 不兼容的指针类型

ios - 如何根据 UIlabel 文本大小动态调整单元格大小

objective-c - 在 Mac 应用程序中移动 shell 脚本创建的文件

objective-c - 将控件/按钮栏添加到配置为源列表的 NSOutlineView 的底部

objective-c - cocoa : is there a way to know if the user has set "Set date & time automatically" in system preferences?

Java 类到 Objective-C 类