我是新手,所以请耐心等待。
我已经创建了一个与图像相关联的动作。
当用户点击图像时,我希望程序打开取景器,允许用户选择图像文件,然后选择一次以在图像中显示该图像。
认为这很简单,但真的很难找到任何支持!
答案 0 :(得分:5)
我不是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
)。