Hi, I need to have a simple example of source-code that implements printing on Mac OS X.
Can somebody give me a link to such sample code?
Thank you.
Can somebody give me a link to such sample code?
Thank you.
//
// PrintTestAppDelegate.m
// PrintTest
//
// Created by kaydell on 4/27/10.
// Copyright 2010 LevSolve.com. All rights reserved.
//
#import "PrintTestAppDelegate.h"
@implementation PrintTestAppDelegate
@synthesize window;
@synthesize printTestView;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
printTestView = [[PrintTestView alloc] init];
}
- (IBAction) print: (id) sender {
NSLog(@"print:");
[printTestView print:nil]; // <<<<<<< this gets called <<<<<<
}
@end
//
// PrintTestView.m
// PrintTest
//
// Created by kaydell on 4/27/10.
// Copyright 2010 LevSolve.com. All rights reserved.
//
#import "PrintTestView.h"
@implementation PrintTestView
- (void)drawString:(NSString *)string centeredInRect:(NSRect)r withAttributes:(NSDictionary *)theAttribute
{
NSPoint stringOrigin;
NSSize stringSize;
stringSize = [string sizeWithAttributes:theAttribute];
stringOrigin.x = r.origin.x + (r.size.width - stringSize.width)/2;
stringOrigin.y = r.origin.y + (r.size.height - stringSize.height)/2;
[string drawAtPoint:stringOrigin withAttributes:theAttribute];
}
- (void) drawRect: (NSRect) dirtyRect { // <<<<<<< this doesn't get called
NSRect bounds = [self bounds];
[self drawString:@"Print test" centeredInRect:bounds withAttributes:nil];
}
@end
printTestView = [[PrintTestView alloc] init];
NSRect frame = NSMakeRect(0.0, 0.0, 100.0, 100.0);
printTestView = [[PrintTestView alloc] initWithFrame:frame];
#import "PrintTestView.h"
@implementation PrintTestView
- (void) print:(id)sender {
NSString *path = [@"~/Logo.png" stringByStandardizingPath];
NSString *string = [NSString stringWithFormat:@"%@%@", @"file://localhost", path];
NSURL *url = [[NSURL alloc] initWithString:string];
NSData *data = [NSData dataWithContentsOfURL:url];
NSImage *image = [[NSImage alloc] initWithData:data];
[self setImage:image];
[super print:sender];
}
@end
Code:NSString *path = [@"~/Logo.png" stringByStandardizingPath];
The following code works, I mean that it prints the image, but I need to get the image from the current directory and I don't know how. (I tried just using the file name for the path name, but that didn't work for some reason.)
![]()
That won't work, because stringByStandardizingPath doesn't expand tildes. You need to use stringByExpandingTildeInPath.
This method can make the following changes in the provided string:
Expand an initial tilde expression using stringByExpandingTildeInPath.