Re: off screen drawing with cocoa
Re: off screen drawing with cocoa
- Subject: Re: off screen drawing with cocoa
- From: Warwick Hall <email@hidden>
- Date: Tue, 24 Apr 2007 14:45:26 +1000
you have given me some ideas, but now i get an exception thrown. how
do i create an offscreen graphics context?
here is my code.
--BEGIN---
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
void usage ( prog )
const char * prog;
{
fprintf( stderr,
"Usage: %s <cid> <wid> <rid> <rdate> <file>\n",
basename( prog ) );
}
NSString * gbUrlEncodeValue ( NSString * iValue )
{
NSString * theRet = nil;
theRet = (NSString *)CFURLCreateStringByAddingPercentEscapes(
kCFAllocatorDefault, (CFStringRef)iValue, NULL,
CFSTR( "?=&+" ), kCFStringEncodingUTF8 );
return [theRet autorelease];
}
void gbGenOrder( NSString * iCid, NSString * iWid, NSString * iRid,
NSString * iRdate,
NSString * iOutput )
{
NSString * thePostString =
[NSString stringWithFormat : @"pass=%@&cid=%@&wid=%@&rid=%@&date=
%@",
gbUrlEncodeValue( (NSString *)CFSTR( "ipo79Tsi" ) ),
gbUrlEncodeValue( iCid ),
gbUrlEncodeValue( iWid ),
gbUrlEncodeValue( iRid ),
gbUrlEncodeValue( iRdate )];
NSData * thePostData =
[thePostString dataUsingEncoding : NSASCIIStringEncoding
allowLossyConversion : YES];
NSString * thePostLength =
[NSString stringWithFormat : @"%d", [thePostData length]];
NSMutableURLRequest * theRequest = [[NSMutableURLRequest alloc]
init];
[theRequest setURL :
[NSURL URLWithString : @"https://wic023u.server-secure.com/
vs250306_secure/admin/batch/formwrapper.php"]];
[theRequest setHTTPMethod : @"POST"];
[theRequest setValue : thePostLength
forHTTPHeaderField : @"Content-Length"];
[theRequest setValue : @"application/x-www-form-urlencoded"
forHTTPHeaderField : @"Content-Type"];
[theRequest setHTTPBody : thePostData];
NSURLResponse * theResponse = nil;
NSError * theError = nil;
NSData * theHtmlData =
[NSURLConnection sendSynchronousRequest : theRequest
returningResponse : &theResponse
error : &theError];
if( theError != nil )
{
[theRequest release]; theRequest = nil;
return;
}
if( theHtmlData == nil )
{
NSLog( @"theHtmlData is nil" );
[theRequest release]; theRequest = nil;
return;
}
NSString * theHtml =
[[NSString alloc] initWithBytes : [theHtmlData bytes]
length : [theHtmlData length]
encoding : NSASCIIStringEncoding];
WebView * webView =
[[WebView alloc] initWithFrame : NSMakeRect( 0.0, 0.0, 300.0,
500.0 )
frameName : nil
groupName : nil];
[webView lockFocus];
[[webView mainFrame] loadHTMLString : theHtml baseURL : nil ];
NSPrintInfo * printInfo = nil;
NSPrintInfo * sharedInfo = nil;
NSPrintOperation * printOp = nil;
NSMutableDictionary * printInfoDict = nil;
NSMutableDictionary * sharedDict = nil;
sharedInfo = [NSPrintInfo sharedPrintInfo];
sharedDict = [sharedInfo dictionary];
printInfoDict = [NSMutableDictionary dictionaryWithDictionary :
sharedDict];
[printInfoDict setObject : [NSNumber numberWithBool : YES]
forKey : NSPrintAllPages];
[printInfoDict setObject : NSPrintSaveJob
forKey : NSPrintJobDisposition];
[printInfoDict setObject : iOutput
forKey : NSPrintSavePath];
printInfo = [[NSPrintInfo alloc] initWithDictionary : printInfoDict];
[printInfo setHorizontalPagination : NSAutoPagination];
[printInfo setVerticalPagination : NSAutoPagination];
[printInfo setVerticallyCentered : NO];
NSMutableData * theData = [[NSMutableData alloc] init];
printOp = [NSPrintOperation EPSOperationWithView : webView
insideRect : [webView bounds]
toData : theData
printInfo : printInfo];
[printOp setShowPanels : NO];
[printOp runOperation];
[printInfo release]; printInfo = nil;
[theHtml release]; theHtml = nil;
[theData writeToFile : iOutput atomically : NO];
[theData release]; theData = nil;
[webView release]; webView = nil;
[theRequest release]; theRequest = nil;
}
int main ( argc, argv )
const int argc;
const char * const * argv;
{
if( argc != 6 )
{
usage( basename( argv[ 0 ] ) );
exit( EXIT_FAILURE );
}
NSApplicationLoad();
NSAutoreleasePool * thePool = [[NSAutoreleasePool alloc] init];
NSGraphicsContext * theGraphicsContext = nil;
NSMutableDictionary * attributes = nil;
attributes = [NSMutableDictionary dictionary];
[attributes setObject : NSGraphicsContextPSFormat
forKey :
NSGraphicsContextRepresentationFormatAttributeName];
theGraphicsContext =
[NSGraphicsContext graphicsContextWithAttributes : attributes];
NSLog( [theGraphicsContext description] );
gbGenOrder( [NSString stringWithCString : argv[ 1 ]],
[NSString stringWithCString : argv[ 2 ]],
[NSString stringWithCString : argv[ 3 ]],
[NSString stringWithCString : argv[ 4 ]],
[[[NSFileManager defaultManager]
stringWithFileSystemRepresentation : (const char *)argv[ 5 ] length :
(unsigned)strlen( argv[ 5 ] )]
stringByExpandingTildeInPath] );
[thePool release]; thePool = nil;
exit( EXIT_SUCCESS );
}
--END--
warwick
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden