2016-11-08 33 views
0

那麼有沒有一種方法可以保存編輯後的圖像,而無需向用戶顯示NSSavePanel?也就是說,許多應用程序爲用戶提供「保存」或「另存爲...」的選項,其中「保存」只是覆蓋保存文件名的文件,「另存爲...」提供完整的NSSavePanel與所有選項。使用ImageKit不使用NSSavePanel節約可可

我有一個應用程序調用ImageKit允許編輯圖像,我希望允許用戶單擊按鈕或按鍵命令以保存而不需要面板。沒有對話,沒有通知,只需點擊保存,圖像文件被新的編輯覆蓋。

我知道如何以這種方式保存文本文件,但我不確定從IKImageView編輯的圖像。

在此先感謝!

回答

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]; 
    }