Trouble creating drag image from a partial view
Trouble creating drag image from a partial view
- Subject: Trouble creating drag image from a partial view
- From: Ken Tozier <email@hidden>
- Date: Fri, 7 Dec 2007 00:03:03 -0500
Hi
I wrote a draggable view class and can successfully create a drag
image of the entire view but am having trouble creating one from just
part of the view. I can't seem to get the right combination of frame,
image size, lock focus etc and am just getting these 1 to 2 pixel
vertical slices of the requested area instead. Examining the
resultant image with NSLog shows that it's dimensions are as expected
but it only contains these tiny vertical slices.
Here's the working code for the drag image of the entire view:
- (NSImage *) dragImageWithAlpha:(float) inAlpha
{
NSRect imgFrame = NSMakeRect(0, 0, [self frame].size.width,
[self frame].size.height);
// create view bitmap
[self lockFocus];
NSBitmapImageRep * dragBitmap = [[NSBitmapImageRep alloc]
initWithFocusedViewRect: imgFrame];
[self unlockFocus];
NSImage *resultImage = [[[NSImage alloc] initWithSize:
imgFrame.size] autorelease];
[resultImage addRepresentation: dragBitmap];
[resultImage lockFocus];
[resultImage drawAtPoint: NSZeroPoint
fromRect: imgFrame
operation: NSCompositeSourceOver
fraction: inAlpha];
[resultImage unlockFocus];
[dragBitmap release];
return resultImage;
}
And here's what I've tried for creating a drag image using a subview
of the draggable view:
(Note: "thumb" is an NSImageView containing a thumbnail of an image
to drag)
- (NSImage *) dragImageWithAlpha:(float) inAlpha
{
// tried both of the following
NSRect thumbFrame = NSMakeRect(0, 0, [thumb frame].size.width,
[thumb frame].size.height);
NSRect thumbFrame = [thumb frame];
// create view bitmap
// tried both of the following with above thumb frames
[self lockFocus];
NSBitmapImageRep *dragBitmap = [[NSBitmapImageRep alloc]
initWithFocusedViewRect: thumbFrame];
[self unlockFocus];
[thumb lockFocus];
NSBitmapImageRep * dragBitmap = [[NSBitmapImageRep alloc]
initWithFocusedViewRect: thumbFrame];
[thumb unlockFocus];
NSImage *resultImage = [[[NSImage alloc] initWithSize:
thumbFrame.size] autorelease];
[resultImage addRepresentation: dragBitmap];
[resultImage lockFocus];
// tried both of the following
[resultImage drawAtPoint: thumbFrame.origin
fromRect: thumbFrame
operation: NSCompositeSourceOver
fraction: inAlpha];
[resultImage drawAtPoint: NSZeroPoint
fromRect: thumbFrame
operation: NSCompositeSourceOver
fraction: inAlpha];
[resultImage unlockFocus];
[dragBitmap release];
return resultImage;
}
Anyone see where I'm going wrong?
Thanks or any help
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