2011-12-13 53 views
1

我有一個NSImageView(在圖像很好)綁定到一個實體的屬性。此屬性是二進制數據,所以我使用的是值轉換器(NSKeyedUnarchiveFromData)。它工作正常,但我希望能夠雙擊圖像並在Preview中打開此圖像。我怎麼做到這一點?如何在預覽中打開圖像,雙擊NSImageView?

回答

1

要在預覽中打開圖像,我很確定您需要先將它保存到磁盤,因爲預覽打開文件。您可以使用此NSImage類方法我寫信給寫出來你的圖像的JPEG:

@implementation NSImage (DFAdditions) 

- (void) saveAsJpegWithPath:(NSString *)filePath { 
    NSData *imageData = [self TIFFRepresentation]; 
    NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData]; 

    CGFloat compressionFactor = 0.8 
    NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:compressionFactor] 
                  forKey:NSImageCompressionFactor]; 
    imageData = [imageRep representationUsingType:NSJPEGFileType properties:imageProps]; 
    [imageData writeToFile:filePath atomically:NO];   
} 

@end 

如果你想有一個無損的TIFF表示,你可以把它降低到一個行實現:

- (void) saveAsTiffWithPath:(NSString *)filePath { 
    [[self TIFFRepresentation] writeToFile:filePath atomically:NO]; 
} 

要在預覽中打開它,你在保存後,我會看看答案在這裏:Cocoa app; open file (pdf) in Preview

它描述瞭如何打開一個PDF,但它應該b對於任何文件都是一樣的。另外,我同意對答案的第一條評論:忽略第一行,並使用常規字符串。基本上,請查看NSWorkspace的文檔。正如Rob Keniger在下面提到的那樣,您可能想要-openFile:withApplication:

+0

特別是,看看`openFile:withApplication:`方法。 – 2011-12-14 03:42:17