使用ImageKit保存Cocoa而不使用NSSavePanel

时间:2016-11-08 01:11:42

标签: xcode macos cocoa save imagekit

那么有没有一种方法来保存编辑过的图像而不向用户呈现NSSavePanel?

也就是说,许多应用程序为用户提供了“保存”或“另存为...”的选项,其中“保存”只是覆盖保存文件名的文件,“另存为...”显示完整的NSSavePanel所有选项。

我有一个应用程序调用ImageKit来允许编辑图像,我想让用户点击按钮或按键命令只需保存而无需面板。没有对话,没有通知,只需单击保存,图像文件将被新编辑的文件覆盖。

我知道如何以这种方式保存文本文件,但我不确定IKImageView编辑过的图像。

提前致谢!

1 个答案:

答案 0 :(得分:0)

所以我只是绕过了NSSavePanel并直接进入GCImageDest:

- (IBAction)saveWithNoPanel: (id)sender
{
    _saveOptions = [[IKSaveOptions alloc] initWithImageProperties: _imageProperties
                                                      imageUTType: _imageUTType];

    NSString * url=[[NSUserDefaults standardUserDefaults] objectForKey:@"photoPath"];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy"];
    NSString * yearString = [formatter stringFromDate:[NSDate date]];

    NSString * fileName = [[_imageWindow representedFilename] lastPathComponent];
    //NSLog(@"Filename: %@",fileName);
    NSString * photoUrl =[NSString stringWithFormat:@"%@%@%@%@%@",url,@"/",yearString,@"/",fileName];
    //NSLog(@"photourl: %@",photoUrl);

    NSString * newUTType = [_saveOptions imageUTType];
    CGImageRef image;

    image = [_imageView image];

        if (image)
        {
            NSURL * url =[NSURL fileURLWithPath: photoUrl];
            //NSLog(@"URL: %@",url);

            CGImageDestinationRef dest = CGImageDestinationCreateWithURL((CFURLRef)url,
                                                                         (CFStringRef)newUTType, 1, NULL);

            if (dest)
            {
                CGImageDestinationAddImage(dest, image,
                                           (CFDictionaryRef)[_saveOptions imageProperties]);
                CGImageDestinationFinalize(dest);
                CFRelease(dest);
            }
        } else
        {
            NSLog(@"*** saveImageToPath - no image");
        }
    [pwPhotoPathTextField setStringValue:[NSString stringWithFormat:@"%@%@",photoUrl]];
    [_imageWindow orderOut:self];
    }