Re: optimizing background image drawing with -drawRect
Re: optimizing background image drawing with -drawRect
- Subject: Re: optimizing background image drawing with -drawRect
- From: Shamyl Zakariya <email@hidden>
- Date: Mon, 2 Apr 2007 14:26:51 -0400
Load the image in -awakeFromNib and hold onto it as a member
variable, draw it in drawRect:
Sort of like so:
@interface MyView : NSView {
NSImage *_backgroundImage;
}
---
- (void) awakeFromNib
{
NSString *BackgroundImageName = [[NSBundle
bundleForClass:@"SpaceOscillatorViewFactory"]
pathForResource:@"SpaceOscillatorUI" ofType:@"png"];
_backgroundImage = [[NSImage alloc]
initWithContentsOfFile:BackgroundImageName];
NSSize BackgroundImageSize = NSMakeSize(763.0, 380.0);
[_backgroundImage setSize:BackgroundImageSize];
}
- (void) drawRect: (NSRect) rect
{
[_backgroundImage drawAtPoint ... ]
}
email@hidden
"go forth and be excellent to eachother"
-- Bill and Ted
On Apr 2, 2007, at 2:08 PM, Artemiy Pavlov wrote:
Hello all!
In the @implementation of my custom view, I have an overridden -
drawRect method which puts a background image:
-(void)drawRect:(NSRect)rect{
NSString *BackgroundImageName = [[NSBundle
bundleForClass:@"SpaceOscillatorViewFactory"]
pathForResource:@"SpaceOscillatorUI" ofType:@"png"];
NSImage *BackgroundImage = [[NSImage alloc]
initWithContentsOfFile:BackgroundImageName];
NSSize BackgroundImageSize = NSMakeSize(763.0, 380.0);
[BackgroundImage setSize:BackgroundImageSize];
[BackgroundImage drawAtPoint:NSMakePoint(0.0, 0.0)
fromRect: NSZeroRect
operation: NSCompositeSourceOver
fraction: 1.0];
}
Everything is working well, but this method gets called every time
any control is moved, and this causes high CPU usage spikes.
My guess is I should load and scale the image once, and use -
drawRect to only do drawAtPoint. But the problem is I cannot move
this part of the code outside -drawRect, I get "error: initializer
element is not constant" for lines with NSString, NSImage, etc.
I would really appreciate any suggestions!
Thanks very much,
Artemiy.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden