Re: drawing view pieces into images
Re: drawing view pieces into images
- Subject: Re: drawing view pieces into images
- From: Public Look <email@hidden>
- Date: Sat, 24 Jan 2004 21:40:20 -0500
There are several ways to do what you want:
image = [[NSImage alloc] initWith
Data:[view
dataWithPDFInsideRect:NSMakeRect(100,150,20,20)]];
- or -
this one taken straight from Apple's docs at
http://developer.apple.com/documentation/Cocoa/Conceptual/DrawImages/
Tasks/CachingRepresentations.html -
NSImage* image = [[NSImage alloc] initWithSize:size];
[image lockFocus];
// draw draw draw
NSBitmapImageRep* rep = [[NSBitmapImageRep alloc]
initWithFocusedViewRect:
NSMakeRect(0,0,size.width,size.height)];
[image unlockFocus];
- or -
the -snapshotFromRect: method in
http://developer.apple.com/samplecode/Sample_Code/Cocoa/Color_Sampler/
NSView_snapshot.m.htm
- (NSImage *) snapshotFromRect:(NSRect) sourceRect;
/*"This method creates a new image from a portion of the receiveing
view. The image is returned autoreleased."*/
{
NSImage
*snapshot = [[NSImage alloc] initWithSize:sourceRect.size];
NSBitmapImageRep
*rep;
[self lockFocus];
rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:sourceRect];
[self unlockFocus];
[snapshot addRepresentation:rep];
return [snapshot autorelease]; // balance the +alloc call..
}
- or -
depending on how you use your temporary image, it might be built in
already -
from
http://developer.apple.com/documentation/Cocoa/Reference/
ApplicationKit/ObjC_classic/Classes/NSWindow.html
cacheImageInRect:
- (void)cacheImageInRect:(NSRect)aRect
Stores the receivers raster image from aRect, which is expressed in
the receivers base coordinate system. This method allows the receiver
to perform temporary drawing, such as a band around the selection as
the user drags the mouse, and to quickly restore the previous image by
invoking restoreCachedImage and flushWindowIfNeeded. The next time the
window displays, it discards its cached image rectangles. You can also
explicitly use discardCachedImage to free the memory occupied by cached
image rectangles. aRect is made integral before caching the image to
avoid antialiasing artifacts.
Only the last cached rectangle is remembered and can be restored.
All of this was found quickly and easily by searching for image and
rect in Apple's developer documentation.
On Jan 24, 2004, at 9:02 PM, Francisco Tolmasky wrote:
>
I'm a little rusty on my drawing commands. What I have is a view of
>
size w,h. I have an image within the view that serves as a buffer.
>
At one point I want to draw the contents of the rect (100,150,20,20)
>
into an image temporarily for later use. So what I do is make an
>
NSImage of size 20,20: NSimage *image= [[NSImage alloc] initWithSize:
>
NSMakeSize(20,20)]; Now what I'[m not sure of is how to tell it to
>
draw what is at 100,150 to the image's 0,0. Something like this is
>
what I'm thinking about:
>
>
[image lockFocus];
>
[image setBoundsOrigin: NSMakePoint(100,150)];
>
[self drawRect: NSMakeRect(100,150,20,20)];
>
[image unlockFocus];
>
>
but image does not have a setBoundsOrigin nor a setBounds. How should
>
I do this?
>
>
Francisco Tolmasky
>
email@hidden
>
http://users.adelphia.net/~ftolmasky
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.