Saving an NSView to a png file?

后端 未结 2 2186
南笙
南笙 2020-12-15 01:16

I am making a simple program that creates game cards for a game I play. I have sent it out to some friends of mine for testing, but they really want it to save images, not j

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 01:59

    First create a TIFF representation of your view:

    // Get the data into a bitmap.
    [self lockFocus];
    rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[self bounds]];
    [self unlockFocus];
    data = [rep TIFFRepresentation];
    

    To support multiple file types, use:

    data = [rep representationUsingType:(NSBitmapImageFileType)storageType
    properties:(NSDictionary *)properties];
    

    NSBitmapImageFileType is an enum constant specifying a file type for bitmap images. It can be NSBMPFileType, NSGIFFileType, NSJPEGFileType, NSPNGFileType, or NSTIFFFileType.

    If you need to customize the NSSavePanel, look into accessory views: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/AppFileMgmt/Articles/ManagingAccessoryViews.html

提交回复
热议问题