Okay, so I'm a newbie trying to write a practice app where you click a button, an NSOpenPanel
appears, you select an image file, and the image gets displayed in an NSImageView
.
I've got the open panel working okay, and it returns an NSArray
of NSURLs
. I avoided using filename paths as the Apple docs said it was depreciated. I then try to make an NSImage
object using initWithContentsOfURL
, and then try to setImage of the NSImageView
to the new image.
Here's what I think are the relevant parts of the implementation...
- (IBAction)openImage:(NSButton *)sender {
NSLog(@"%@ was clicked", sender);
NSOpenPanel *panel = [[NSOpenPanel alloc] init];
if ([panel runModal] == NSModalResponseOK)
{
NSArray* selectedFile = [panel URLs];
NSLog(@"%@ was selected", selectedFile[0]);
NSImage *theImage = [[NSImage alloc] initWithContentsOfURL:selectedFile[0]];
[imageViewer setImage:theImage];
}
}
And from the header:
@property (weak) IBOutlet NSImageView *imageViewer;
- (IBAction)openImage:(NSButton *)sender;
@end
When I try to set the image, Xcode says there's imageViewer
is undeclared, and wants to correct it to _imageViewer
. I tried following its advice and ran it, but then the app just crashes once I select a file from the open panel so something is obviously still not right.
The NSLog
lines shows the button was clicked, and shows the correct URL of the file selected, but I'm having issues setting the NSImageView
. It's probably something really simple but I can't seem to figure it out.