Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
my attempts to copy file contents to the pasteboard are unsuccessful... i've read the documentation on dragging file contents and promised files and all that...

i have a view (which is just a color), when i drag the mouse over the view, the program makes a .png file from cropping the view and writing that file to the tmp folder. then the program is *suppose* to copy that file to the pasteboard using the writeFileContents method. it doesn't copy to the pasteboard because i can't seem to drop it on my desktop, or anywhere outside of the program that should accept .PNG files for that matter. it will just always slideback.

what am i doing wrong here?

Code:
#pragma mark Dragging Source

- (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal
	{
	return NSDragOperationCopy;
	}

- (void)mouseDown:(NSEvent *)event
	{
	[event retain];
	[mouseDownEvent release];
	mouseDownEvent = event;
	}
	
- (BOOL)acceptsFirstMouse:(NSEvent *)event
	{
	return YES;
	}

- (void)swatchDragWithStroke:(NSRect)rect
	{
	[fillColor setFill];
	NSRectFill(rect);
	
	NSBezierPath *pathThickness = [NSBezierPath bezierPathWithRect:rect];
	[pathThickness setLineWidth:4];
 
	NSImage *dottedStroke = [NSImage imageNamed:@"DottedStroke.png"];
	NSColor *dottedColor = [NSColor colorWithPatternImage:dottedStroke];

	[dottedColor setStroke];
	[pathThickness stroke];
	}
	
	
- (void)mouseDragged:(NSEvent *)event
	{
	NSPoint down = [mouseDownEvent locationInWindow];
	NSPoint drag = [event locationInWindow];
	float distance = hypot(down.x - drag.x, down.y - drag.y);
		if (distance < 2)
		{
		return;
		}
	
	NSRect swatchDrag;
	NSSize size = NSMakeSize(64, 64);
	swatchDrag.origin = NSZeroPoint;
	swatchDrag.size = size;

	NSImage *anImage = [[NSImage alloc] initWithSize:size];

	[anImage lockFocus];
	[self swatchDragWithStroke:swatchDrag];
	[anImage unlockFocus];
	
	NSString *pathName = @"/tmp/Swatch.png";
	
	NSRect croppedRect = NSMakeRect(0, 0, 16, 16);
	[self lockFocus];
	NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:croppedRect];
	[self unlockFocus];
	NSData *data = [rep TIFFRepresentation];
	[data writeToFile:pathName atomically:YES];
	[NSBitmapImageRep release];


	NSPoint point = [self convertPoint:down fromView:nil]; 
	point.x -= size.width / 2.0;
	point.y -= size.height / 2.0;
	
	NSPasteboard *pb = [NSPasteboard pasteboardWithName:NSDragPboard];
	
	[pb writeFileContents:pathName];

	[self dragImage:anImage at:point offset:NSMakeSize(0,0) event:mouseDownEvent pasteboard:pb source:self slideBack:YES];
	[anImage release];
	}
 

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
Is it valid to write TIFF data to a PNG?

this seems to work to copy the data onto the pasteboard, and drop onto the desktop (or Preview)...

Code:
NSPasteboard *pboard = [NSPasteboard generalPasteboard];
[pboard declareTypes:[NSMutableArray arrayWithObject:NSTIFFPboardType] owner:nil];
[pboard setData:data forType:NSTIFFPboardType];

but i'd like to be able to drop it on the desktop as an actual .png or .tif image file (instead of a picture clipping), or into Photoshop, but i think i have to use drag file promises for that, and that doesn't seem to allow for a customized drag image.
 

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
oh... nevermind... i forgot to add this:

Code:
[pboard setPropertyList:[NSArray arrayWithObject:pathName] forType:NSFilenamesPboardType];

works now :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.