Reputation: 1
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.
Upvotes: 0
Views: 1916
Reputation: 1
Okay, so I feel stupid now. I'm still learning stuff so I wasn't sure why it was freezing up until I noticed Xcode kept switching to the "Debug" panel, which clued me into what might be happening.
I figured out I accidentally clicked a line number, which apparently sets breakpoints for debugging. I also might have somehow clicked "Toggle global breakpoint state" in the bottom Debug area.
Because I selected the line where the setImage
was, it looked like the app crashed/froze whenever it hit that line, but it actually just paused the app to allow me to look at what's going on with the app and debug it. I deselected the lines and turned off the global breakpoint state button and everything runs fine now.
A newbie coder mistake from somebody not used to working in an IDE. :P
Upvotes: 0
Reputation: 47169
You need to declare the variable imageViewer
in your headers (.h
) interface
, for example:
@interface AppDelegate : NSObject <NSApplicationDelegate> {
IBOutlet NSImageView *__weak imageViewer;
}
@property (weak) IBOutlet NSImageView *imageViewer;
- (IBAction)openImage:(NSButton *)sender;
@end
Then in your class (.m
) just synthesize the variable:
@implementation AppDelegate
@synthesize imageViewer;
...
Since you hadn't declared
the variable in the interface
you were receiving the error.
Upvotes: 1
Reputation: 186
Try this code.
- (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]);
NSURL *url = (NSURL *)selectedFile[0];
NSString *filePath = [url absoluteString];
NSImage *theImage = [[NSImage alloc] initWithContentsOfFile:filePath];
[imageViewer setImage:theImage];
}
}
Upvotes: 0