macos - OSX 打开取景器并显示图像

标签 macos cocoa

我对此很陌生,所以请耐心等待。

我已经创建了一个与图像良好链接的操作。

当用户单击图像井时,我希望程序打开Finder,允许用户选择一个图像文件,然后一旦选择就在图像井中显示该图像。

以为这会很简单,但真的很难找到任何支持!

最佳答案

我不是 100% 确定我遵循“打开取景器”的意思,而不是将焦点从您的应用程序切换到取景器,我认为您需要这样的东西:

- (IBAction)clickOnImageView:(id)sender {

//create open panel...
NSOpenPanel* openPanel = [NSOpenPanel openPanel];

//set restrictions / allowances...
[openPanel setAllowsMultipleSelection: NO];
[openPanel setCanChooseDirectories:NO];
[openPanel setCanCreateDirectories:NO];
[openPanel setCanChooseFiles:YES];
    //only allow images...
[openPanel setAllowedFileTypes:[NSImage imageFileTypes]];

//open panel as sheet on main window...
[openPanel beginSheetModalForWindow:[self window] completionHandler:^(NSInteger result) {

    if (result == NSFileHandlingPanelOKButton) {

        //get url (should only be one due to restrictions)...
        for( NSURL* URL in [openPanel URLs] ) {

            //get NSImage from path...
            NSImage* selectedImage = [[NSImage alloc] initWithContentsOfFile:[URL path]];

            [imageView setImage:selectedImage];

        }

    }
}];

}

基本上是一个 NSOpenPanel,您只能在其中选择图像文件,并且生成的图像将放置在 NSImageView 中(在此示例中 imageView)。

关于macos - OSX 打开取景器并显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20305349/

相关文章:

swift - 如何在不改变位置的情况下改变窗框大小

macos - 使用 Delphi XE2 在 MacOS 应用程序中嵌入 Web 浏览器

macos - Snow Leopard 的新 "sudden termination"机制有缺点吗?

python - iPython 不读取 ~/.inputrc

objective-c - 从 Cocoa 应用程序执行终端命令 top

cocoa - 具有比例变换的模糊 CATextLayer

objective-c - 在任何FSEvent或计时器触发之前,NSRunLoop会提前返回

android - 德尔福 XE5 Firemonkey : Single code base for mobile/Win/OSX?

cocoa - 更新的 NSTableCellView.textField 未在表格显示中更新。

cocoa - 创建 CGImageRef 而不缓存任何内容