Re: Draggable NSImage of custom NSView ???
Re: Draggable NSImage of custom NSView ???
- Subject: Re: Draggable NSImage of custom NSView ???
- From: Carlos Weber <email@hidden>
- Date: Thu, 17 Jan 2002 08:17:48 -1000
On Thursday, January 17, 2002, at 04:31 , Robert Miller wrote:
>
Hello fellow Cocoans,
>
>
This is probably in the realm of Cocoa 101 but.... I'm trying to
>
obtain a drag image of a custom view and its subviews by first creating
>
an NSImage that is the same size as my custom view, locking focus on the
>
NSImage, and drawing my custom view using its display method ex.
>
>
- (NSImage*) makeMyDragImage
>
{
>
NSImage *dragImg = [[NSImage alloc] initWithSize:[self frame].size];
>
>
[dragImg lockFocus];
>
[self display];
>
[dragImg unlockFocus];
>
return dragImg;
>
}
>
>
of course the image is always empty and nothing gets drawn into it
>
probably because the display call unfocuses the image and focuses
>
'self'. So that's the $64,000.00 question. How do I draw my custom view
>
and its subviews into an NSImage so that I can obtain a draggable image
>
representing the view. (Similar to dragging a build phase segment in PB)
>
???? PS. I've also tried NSCopyBits using [self gState] as the argument
>
in place of calling display and that has no effect either ???
I haven't done this myself, but the following method from
NSBitMapImageRep sounds like it might serve:
- (id)initWithFocusedViewRect:(NSRect)rect
Initializes the receiver, a newly allocated NSBitmapImageRep object,
with bitmap data read from a rendered image. The image that's read is
located in the current window and is bounded by the rect rectangle as
specified in the current coordinate system.
This method uses imaging operators to read the image data into a buffer;
the object is then created from that data. The object is initialized
with information about the image obtained from the Window Server.
If for any reason the new object can't be initialized, this method frees
it and returns nil. Otherwise, it returns the initialized object (self).