从NSImageView实施拖动并将图像保存到文件

时间:2011-10-16 02:34:24

标签: cocoa drag-and-drop nsimageview

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

有人可以举个例子吗?

2 个答案:

答案 0 :(得分:9)

我终于找到了怎么做。我在Github上写了demo

答案 1 :(得分:-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