Re: Drawing inside an NSImage?
Re: Drawing inside an NSImage?
- Subject: Re: Drawing inside an NSImage?
- From: Ken Thomases <email@hidden>
- Date: Sun, 08 Mar 2015 19:15:07 -0500
On Mar 8, 2015, at 6:12 PM, Patrick J. Collins <email@hidden> wrote:
> I have an NSView which has in its "drawRect:" method:
>
> NSColor *backgroundColor = [NSColor colorWithPatternImage:self.image];
> [backgroundColor setFill];
>
> ...
>
> the image method does:
>
> -(NSImage *)image {
> if (!_image) {
> _image = [[NSImage alloc] initWithSize:self.bounds.size];
>
> [_image lockFocus];
> [[NSColor blueColor] set];
> NSBezierPath *line = [NSBezierPath bezierPath];
>
> NSPoint pointA = NSMakePoint(0, 0);
> NSPoint pointB = NSMakePoint(self.bounds.size.width, 0);
>
> [line moveToPoint:pointA];
> [line lineToPoint:pointB];
> [line stroke];
> }
> return _image;
> }
>
> The resulting line is drawn off center (somewhere around -10px) from the middle
> of the NSView... I would like to have my 0,0 origin be the bottom left corner
> of the NSView as I would get if I were to put this line directly in the
> drawRect (not using an NSImage): method..
>
> What am I doing wrong here?
The drawing of a pattern is governed by the graphics context's patternPhase. In your view's -drawRect:, you may need to do [NSGraphicsContext currentContext].patternPhase = NSZeroPoint. Or possibly, it may need to be set to the view's origin in window coordinates ([self convertPoint:self.frame.origin toView:nil]).
Also, you only create the image once, the first time it is requested. The image's size is fixed to the view's bounds' size as it was at that time. If the view changes size after that, the image size will no longer match the view's size unless you invalidate the cache so it is recreated the next time it is needed.
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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