4

我研究过苹果的例子:CocoaDragAndDrop。我了解如何将图像拖动到 NSImageView 并在视图之间拖动图像。但是,当图像被拖放到 Finder 上时,我仍然不知道如何拖动图像并将其保存到文件中。

谁能给我一个例子?

4

2 回答 2

9

我终于找到了怎么做。我在 Github 上写了一个演示

于 2011-10-20T03:16:35.227 回答
-2
-(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
于 2011-10-16T02:51:05.620 回答