我对此很陌生,所以请耐心等待。
我已经创建了一个与图像良好链接的操作。
当用户单击图像井时,我希望程序打开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/