copy everything in image view and export to jpeg.

ok, well for my next bit of software i am making a collectable card game card maker. Basically you have blank card templates and a whole bunch of text fields that place all the cards info. I am guessing you will need some obj-c coding to accomplish what i want to do, and i do not know any obj-c at all. So heres how it works. You create the image few for the blank card template. There is a text field to type in the name of your card. You also have a text field on the blank template that will read whatever they typed in as the card name. Now how would i get it so the blank card template along with the new text is exported as a jpg?

The fastest way I can think of to composite an NSView hierarchy is using NSBitmapImageRep. The following code is completely untested. You may have to tinker with it. (For example, I used PNG because they’re good on file size and look better than JPEGs with flat areas of colour. It’s trivial to change it to JPEG if you require.)

However, if you have no Objective-C experience, you may not understand what’s going on here. I urge you to take the time to learn how this works before using it. You can’t fix bugs that come up or make changes if you can’t describe what’s happening. You’ll also need to learn how to hook this up in Xcode and Interface Builder, and since I’ve done the heavy lifting, I’m leaving that as a learning exercise for you.

Think of this as a great, simple project to start learning Cocoa and Obj-C. :slight_smile:

Edit: I have no idea how to get tab stops or multiple spaces to work in BBCode, so here’s a formatted version:

http://www.mikey-san.net/CardCompositorController.m

[code]@interface CardCompositorController : NSObject
{
IBOutlet id parentWindow; // window to which you wish to attach the save panel sheet
IBOutlet id yourCardView; // highest-level view containing the card
}
//=============================================================
// callback for taking action after dismissing the save panel.
// invokes -[NSBitmapImageRep writeToFile:atomically:] on the
// NSBitmapImageRep returned from -compositedCard
//=============================================================

  • (void)savePanelDidEnd:(NSSavePanel *)savePanel returnCode:(int)returnCode contextInfo:(void *)contextInfo

//=============================================================
// initiates -savePanelDidEnd:returnCode:contextInfo: callback.
//=============================================================

  • (IBAction)saveCard:(id)sender;

//=============================================================
// heavy lifting. composites the highest-level card view into
// bitmap data.
//=============================================================

  • (NSBitmapImageRep *)compositedCard;

@end

@implementation CardCompositorController

  • (void)savePanelDidEnd:(NSSavePanel *)savePanel returnCode:(int)returnCode contextInfo:(void *)contextInfo
    {
    if (returnCode == NSOKButton)
    {
    // NSBitmapImageRep has a convenient method for saving its data directly to disk
    [[[self compositedCard] representationUsingType:NSPNGFileType properties:nil writeToFile:[savePanel filename] atomically:YES];
    }
    }

  • (IBAction)saveCard:(id)sender
    {
    NSSavePanel *savePanel = [NSSavePanel savePanel];

    [savePanel setRequiredFileType:@“png”];
    [savePanel setCanSelectHiddenExtension:YES];
    [savePanel beginSheetForDirectory:[savePanel directory]
    file:@“untitled card”
    modalForWindow:parentWindow
    modalDelegate:self
    didEndSelector:@selector(savePanelDidEnd:returnCode:contextInfo:)
    contextInfo:nil];
    }

  • (NSBitmapImageRep *)compositedCard
    {
    NSBitmapImageRep *card;
    NSRect cardBounds;

    cardBounds = [yourCardView bounds];

    [yourCardView lockFocus]; // lock onto the context of the card view
    card = [[NSBitmapImageRep alloc] initWithFocusedViewRect:cardBounds]; // composite the contents of the view into bitmap data
    [yourCardView unlockFocus];

    return [card autorelease];
    }

@end[/code]