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

发布于 2024-12-10 07:16:41 字数 130 浏览 0 评论 0原文

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

有人能给我举个例子吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眉黛浅 2024-12-17 07:16:41

我终于知道该怎么做了。我在 Github 上写了一个 演示

I finally found out how to do it. And I wrote a demo on Github

阳光的暖冬 2024-12-17 07:16:41
-(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
-(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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文