• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Context and the drawing of NSImages
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Context and the drawing of NSImages


  • Subject: Context and the drawing of NSImages
  • From: Brant Sears <email@hidden>
  • Date: Wed, 13 Aug 2008 13:30:22 -0400

Hi. I've run into some behavior that I don't understand.

I'm trying to draw a series of NSImages programatically and then cache them into an NSArray.

The problem I'm having is that some of the operations I'm doing on one image seem to have an effect on some subsequent images in the series.

The way I'm creating the images is by initializing a new NSImage object, calling lockFocus on it, doing the drawing, calling unlockFocus, then returning the image and adding it to my array.

The call that seems to be the problem is [[NSView focusView] setBoundsOrigin:]. It appears from the results I'm getting that these changes seem to "reset" when the instance of NSImageCacheView changes. It seems to be the case that the focusView and the NSImage object are not the same thing. I tried calling recache on the image after I create it which had no effect.

What I am doing wrong? How can I control the resetting of the graphics context? I'm intending to draw images that involve several operations on the context. This is just a simplified example.

Thanks.

// this is the code that makes the images and adds them to the array.
for (i=0;i<kNumberOfImages;++i) {
	NSImage * myImage = [self imageForIndex:i];

	if (myImage) {
		[imgArray addObject: myImage];
	}
}

// this is the method that makes the image.
- (NSImage*)imageForIndex:(int)index
{
	float w = 128;
	float h = 128;
	[NSGraphicsContext saveGraphicsState];
	NSImage *myImage = [[NSImage alloc] initWithSize:NSMakeSize(w, h)];
    [myImage lockFocus];
	NSLog([[NSView focusView] description]);

	NSColor * myColors[] = {
        [NSColor redColor]
        ,[NSColor greenColor]
        ,[NSColor blueColor]
        ,[NSColor cyanColor]
        ,[NSColor yellowColor]
        ,[NSColor magentaColor]
        ,[NSColor orangeColor]
        ,[NSColor purpleColor]
    };
	int numColors = sizeof(myColors) / sizeof(NSColor);

	while (index >= numColors)
		index -= numColors;

	[myColors[index] set];

	NSRectFill(NSMakeRect(0,0,w,h));

NSPoint myPoint = NSMakePoint(-1 * (w/2), -1 * (h/2));
[[NSView focusView] setBoundsOrigin:myPoint];
[[NSColor blackColor]set];
NSRect circleRect = NSMakeRect(-20,-20,40,40);
NSBezierPath * circlePath = [NSBezierPath bezierPathWithOvalInRect: circleRect];
[circlePath fill];


	[myImage unlockFocus];

	[NSGraphicsContext restoreGraphicsState];

	return myImage;
}

_______________________________________________

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


  • Prev by Date: Problem with NSImage and .PNG file
  • Next by Date: problem with DirectoryServices
  • Previous by thread: Re: Problem with NSImage and .PNG file
  • Next by thread: problem with DirectoryServices
  • Index(es):
    • Date
    • Thread