将NSView保存到png文件?

我正在制作一个简单的程序,为我玩的游戏创建游戏卡。我已将它发送给我的一些朋友进行测试,但他们真的希望它能保存图像,而不仅仅是打印它们。我试图将它保存为.png文件。我在这里有问题。 如何将其保存为.png文件,包括所有视图的NSImageWells。 如何将NSPopupButton添加到NSSavePanel以允许用户选择格式? 任何帮助是极大的赞赏。     
已邀请:
首先创建视图的TIFF表示:
// Get the data into a bitmap.
[self lockFocus];
rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[self bounds]];
[self unlockFocus];
data = [rep TIFFRepresentation];
要支持多种文件类型,请使用:
data = [rep representationUsingType:(NSBitmapImageFileType)storageType
properties:(NSDictionary *)properties];
NSBitmapImageFileType是一个枚举常量,指定位图图像的文件类型。它可以是NSBMPFileType,NSGIFFileType,NSJPEGFileType,NSPNGFileType或NSTIFFFileType。 如果您需要自定义NSSavePanel,请查看附件视图:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/AppFileMgmt/Articles/ManagingAccessoryViews.html     
// Get the data into a bitmap.
[viewBarChart lockFocus];
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[viewBarChart bounds]];
[viewBarChart unlockFocus];
NSData *exportedData = [rep representationUsingType:NSJPEGFileType properties:nil];

NSSavePanel *savepanel = [NSSavePanel savePanel];
savepanel.title = @"Save chart";

[savepanel setAllowedFileTypes:[NSArray arrayWithObject:@"jpg"]];

[savepanel beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger result)
 {
     if (NSFileHandlingPanelOKButton == result)
     {
         NSURL* fileURL = [savepanel URL];

         if ([fileURL.pathExtension isEqualToString:@""])
             fileURL = [fileURL URLByAppendingPathExtension:@"jpg"];

         [exportedData writeToURL:fileURL atomically:YES];
     }

     [rep release];
 }];
    

要回复问题请先登录注册