我研究过苹果的例子:CocoaDragAndDrop。我了解如何将图像拖动到 NSImageView 并在视图之间拖动图像。但是,当图像被拖放到 Finder 上时,我仍然不知道如何拖动图像并将其保存到文件中。
谁能给我一个例子?
我研究过苹果的例子:CocoaDragAndDrop。我了解如何将图像拖动到 NSImageView 并在视图之间拖动图像。但是,当图像被拖放到 Finder 上时,我仍然不知道如何拖动图像并将其保存到文件中。
谁能给我一个例子?
我终于找到了怎么做。我在 Github 上写了一个演示
-(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