Implement drag from NSImageView and save image to a file
-
10-02-2021 - |
Question
I've studied Apple's example: CocoaDragAndDrop. I understand how to drag a image to NSImageView and drag an image between views. However, I still don't known how to drag an image and save it to a file when the image is dropped onto the Finder.
Can anybody give me a example?
Solution
I finally found out how to do it. And I wrote a demo on Github
OTHER TIPS
-(IBAction)saveImageButtonPushed:(id)sender
{
NSLog(@"saveImageButtonPushed");
NSBitmapImageRep *rep;
NSData *data;
NSImage *image;
[self lockFocus];
rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[self frame]];
[self unlockFocus];
image = [[[NSImage alloc] initWithSize:[rep size]] autorelease];
[image addRepresentation:rep];
data = [rep representationUsingType: NSPNGFileType properties: nil];
//save as png but failed
[data writeToFile: @"asd.png" atomically: NO];
//save as pdf, succeeded but with flaw
data = [self dataWithPDFInsideRect:[self frame]];
[data writeToFile:@"asd.pdf" atomically:YES];
}
//......
@end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow