• 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
Re: Creating drag images from NSView snapshots
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Re: Creating drag images from NSView snapshots (From: Ron Fleckner <email@hidden>)
 >Re: Creating drag images from NSView snapshots (From: Ken Tozier <email@hidden>)
 >Re: Creating drag images from NSView snapshots (From: Dominik Pich <email@hidden>)
 >Re: Creating drag images from NSView snapshots (From: Dorian Johnson <email@hidden>)

  • Prev by Date: Cross NIB connections in IB
  • Next by Date: Re: Adding Images to a Cocoa Application in XCode
  • Previous by thread: Re: Creating drag images from NSView snapshots
  • Next by thread: Re: Astoundingly dumb bindings question - solved, simple as expected
  • Index(es):
    • Date
    • Thread