Re: Creating drag images from NSView snapshots
Re: Creating drag images from NSView snapshots
- Subject: Re: Creating drag images from NSView snapshots
- From: Ken Tozier <email@hidden>
- Date: Sun, 1 Jul 2007 17:45:41 -0400
Thanks Dorian. Works great!
Ken
Here's the complete solution for others that might be looking how to
do the same thing
Add the following to NSView subclasses that you want to have a drag
image
- (void) mouseDragged:(NSEvent *) inEvent
{
if ((selected == YES) && (isDraggable == YES))
{
NSImage *selfImage = [self dragImageWithAlpha: .5];
NSPasteboard *pboard = [NSPasteboard
pasteboardWithName:NSDragPboard];
[pboard declareTypes: [NSArray arrayWithObject: NSTIFFPboardType]
owner: self];
[pboard setData:[selfImage TIFFRepresentation] forType:
NSTIFFPboardType];
[self dragImage: selfImage
at: NSMakePoint(0, 0)
offset: NSMakeSize(0.0, 0.0)
event: inEvent
pasteboard: pboard
source: self
slideBack: YES];
}
}
- (NSImage *) dragImageWithAlpha:(float) inAlpha
{
NSRect imgFrame = NSMakeRect(0, 0, [self frame].size.width,
[self frame].size.height);
[self lockFocus];
NSBitmapImageRep *tempBitmap = [[[NSBitmapImageRep alloc]
initWithFocusedViewRect: imgFrame] autorelease];
[self unlockFocus];
NSImage *tempImage = [[[NSImage alloc] initWithSize:
imgFrame.size] autorelease],
*resultImage = [[[NSImage alloc] initWithSize: imgFrame.size]
autorelease];
[tempImage addRepresentation: tempBitmap];
[resultImage lockFocus];
[tempImage drawAtPoint: NSZeroPoint
fromRect: NSMakeRect(0, 0, imgFrame.size.width, imgFrame.size.height)
operation: NSCompositeSourceOver
fraction: inAlpha];
[resultImage unlockFocus];
return resultImage;
}
On Jul 1, 2007, at 3:51 PM, Dorian Johnson wrote:
@implementation NSImage (DJAdditions)
- (NSImage *)imageWithFraction:(float)fraction
{
NSImage *newImage = [[[NSImage alloc] initWithSize:[self size]]
autorelease];
[newImage lockFocus]; {
[self drawAtPoint:NSZeroPoint fromRect:(NSRect){NSZeroPoint,
[self size]} operation:NSCompositeSourceOver fraction:fraction];
} [newImage unlockFocus];
return newImage;
}
@end
_______________________________________________
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