问题描述:

Im new to this so please bear with me.

Ive got an action created linked to an image well.

When the user clicks the image well, i want the programme to open finder, allow the user to select an image file, then once selected to display this image in the image well.

Thought it would be quite simple but really struggling to find any support for this!

网友答案:

I'm not 100% sure I follow what you mean by 'open finder', rather than switch focus from your app to the finder I think you need something like this:

- (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];

        }

    }
}];

}

basically an NSOpenPanel where you can choose only image files and the resulting image will be placed in the NSImageView (in this example imageView).

相关阅读:
Top