I am pretty new in coding in objective-c and I have been stucked for a couple of hours with a view management issue. Based on Matt Gemmel RoudedFloatingPannel, I would like to display a NSImage with a nice semi-transparent rounded background.
//CREATE THE TRANSPARENT WINDOW
window = [[TransparentWindow alloc] initWithContentRect:contentRect styleMask:NSBorderlessWindow开发者_如何学CMask backing:NSBackingStoreBuffered defer:NO];
//ADD THE ROUNDED BACKGROUND
[window setContentView:[[RoundedView alloc] init]];
//GET RUNNING APPLICATION
NSArray *RunningApps = [[NSWorkspace sharedWorkspace] runningApplications];
//PREPARE TEST IMAGE VIEW
NSImage *image = [[RunningApps objectAtIndex:9] icon];
NSImageView *img = [[NSImageView alloc] init];
[img setImage:image];
//DISPLAY THE ICON
[[window contentView] addSubview:img];
It seems I am doing something wrong, as I only get the background displayed. Could someone help me on this ?
Thanks in advance. Gael.
First of all, you are leaking some objects:
[window setContentView:[[RoundedView alloc] init]];
...
NSImageView *img = [[NSImageView alloc] init];
those should be autoreleased.
As to your specific issue, I would suggest stepping through the code with the debugger and check that each statement executes as expected (e.g., that all the objects you allocate are not nil). This will help hunting down the problem.
There are a couple of surprising things in your code.
As you apparently subclassed NSWindow
(TransparentWindow
?), you'd maybe better add it a property which you can set just after the alloc/init sequence.
You could also add this property to the apparent subclass of NSView
(RoundedView
?) you provide.
Your comment suggests you're making a test getting the icon
property of some application.Try adding an image into your bundle, it will easier to load with something like :
[[NSBundle mainBundle] pathForResource:@"imageFilename"
ofType:@"png"];
, where PNG format of course is an example.
Hope this helps.
Sergio is right, I had to manage my views' frames:
NSImageView *img = [[[[NSImageView alloc] initWithFrame:[[window contentView] frame]] retain] autorelease];
精彩评论