6

私がプレイするゲームのゲーム カードを作成する簡単なプログラムを作成しています。私はテストのために私の友人にそれを送りましたが、彼らは本当に画像を印刷するだけでなく、画像を保存することを望んでいます. .png ファイルとして保存しようとしました。ここで質問があります。

  • ビューのすべての NSImageWells を含む、ビューを .png ファイルとして保存するにはどうすればよいですか。

  • NSPopupButton を NSSavePanel に追加して、ユーザーが形式を選択できるようにするにはどうすればよいですか?

どんな助けでも大歓迎です。

4

2 に答える 2

12

まず、ビューの 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

于 2011-03-30T20:25:28.420 に答える
4
// 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];
 }];
于 2013-05-18T12:07:22.407 に答える