Re: Composing an NSImage to print
Re: Composing an NSImage to print
- Subject: Re: Composing an NSImage to print
- From: Ken Ferry <email@hidden>
- Date: Sat, 15 Jan 2011 13:18:52 -0800
Hi Olivier,
Unless I misread this, this section cannot be doing anything helpful:
NSImageView *tempIm = [[NSImageView alloc] initWithFrame:frontRect];
> [tempIm setImage:[cusThumbFront image]];
>
> NSData* theData = [tempIm dataWithPDFInsideRect:[tempIm bounds]];
> NSPDFImageRep* pdfRep = [NSPDFImageRep imageRepWithData:theData];
>
> // Create a new image to hold the PDF representation.
> NSImage* pdfImage = [[NSImage alloc] initWithSize:IMG_SIZE];
> [pdfImage addRepresentation:pdfRep];
Have you tried just using [cusThumbFront image] instead of going through
this? The system cannot create data where there isn't any, so the pdfImage
cannot contain any data that wasn't already in the cusThumbFrontImage.
Okay, that aside, it happens I haven't implemented printing before, so
forgive me if it turns out this is a non-standard way to do things.
It is the subview hierarchy that is printed. If you want to put a watermark
on top of an image, have a view that draws the base image and then draws the
watermark image. Or, make a subview of the view that draws the base that
draws the watermark.
It is possible to produce an NSImage that has the watermark and still has
all the quality available in the original, I just suspect it isn't the most
straightforward way to do what you'd like to here.
The way you'd make such an image is to subclass NSImageRep. Subclassing
NSImageRep is pretty much the same as subclassing NSView. Where in NSView
you override drawRect:, in NSImageRep you override -draw. The main
difference between an image and a view is in the guarantees on how they
scale. If you stretch a button horizontally, the text doesn't stretch out,
it stays centered. An image scales… like an image (i.e. linearly).
So here's how you might make an NSImageRep that drew a base image and an
overlay without rasterizing anything, even though I think this isn't the
best way to solve your problem.
@interface CompositeImageRep : NSImageRep {
NSImage *_baseImage;
NSImage *_overlayImage;
NSRect _overlayFrame;
}
- (id)initWithBaseImage:(NSImage *)baseImage
overlayImage:(NSImage*)overlayImage overlayFrame:(
NSRect)overlayFrame;
@property (readonly) NSImage *baseImage;
@property (readonly) NSImage *overlayImage;
@property (readonly) NSRect overlayFrame;
@end
@implementation CompositeImageRep
- (id)initWithBaseImage:(NSImage *)baseImage
overlayImage:(NSImage*)overlayImage overlayFrame:(
NSRect)overlayFrame {
NSParameterAssert(baseImage != nil);
self = [super init];
if (self) {
{
_baseImage = [baseImage retain];
_overlayImage = [overlayImage retain];
_overlayFrame = overlayFrame;
[self setSize:[baseImage size]];
}
}
return self;
}
-(void)dealloc {
[_baseImage release];
[_overlayImage release];
[super dealloc];
}
- (BOOL)draw {
NSRect bounds = (NSRect){NSZeroPoint, [self size]};
[_baseImage drawInRect:bounds fromRect:NSZeroRect operation:
NSCompositeSourceOver fraction:1.0];
[_overlayImage drawInRect:_overlayFrame fromRect:NSZeroRect operation:
NSCompositeSourceOver fraction:1.0];
return YES;
}
- (id)copyWithZone:(NSZone *)zone {
CompositeImageRep *rep = [super copyWithZone:zone];
// careful - superclass uses NSCopyObject. :-(
rep->_baseImage = [_baseImage retain];
rep->_overlayImage = [_baseImage retain];
rep->_overlayFrame = _overlayFrame;
return rep;
}
@synthesize baseImage=_baseImage, overlayImage=_overlayImage,
overlayFrame=_overlayFrame;
@end
On Tue, Jan 11, 2011 at 7:57 AM, Olivier Palliere <
email@hidden> wrote:
> Hi Guys,
>
>
>
> I'm working on an app for my personal use that needs to print images I drag
> on an NSImageView to display a thumbnail.
>
>
>
> Source images are about 500x500 at 300dpi for most.
>
>
>
> When I want to print them, I use this snippet of code from Apple's
> doc:
>
> frontRect = NSMakeRect(0,0,500, 500);
>
>
>
> NSImageView *tempIm = [[NSImageView alloc] initWithFrame:frontRect];
> [tempIm setImage:[cusThumbFront image]];
>
> NSData* theData = [tempIm dataWithPDFInsideRect:[tempIm bounds]];
> NSPDFImageRep* pdfRep = [NSPDFImageRep imageRepWithData:theData];
>
> // Create a new image to hold the PDF representation.
> NSImage* pdfImage = [[NSImage alloc] initWithSize:IMG_SIZE];
> [pdfImage addRepresentation:pdfRep];
>
>
>
> When I add this image to a NSImageView and give it to a printOperation, it
> is printed crystal clear with the right resolution.
>
>
>
> If I try however, to draw this image in a new one to add my borders like
> this:
>
>
> [compositeImage lockFocus];
> [pdfRep drawInRect:frontRect fromRect:NSZeroRect
> operation:NSCompositeSourceOver
> fraction:1.0];
>
> fpath = [NSBezierPath bezierPathWithRect:frontRect];
> [fpath setLineWidth:1];
> [[NSColor blackColor] set];
> [fpath stroke];
> [compositeImage unlockFocus];
>
>
>
> (I tried to drawInRect both the PDFImageRep or the NSImage directly with
> the same failed results)
>
>
>
> The resulting image looses the printing resolution and the printed image
> is blurry. I read similar posts that explains that
> lockFocus/unlockFocus rasterizes the image, and that I should use PDFKit
> but the doc on that one is sparse (to me at least). I also found some
> post indicating I should use bestRepresentationForDevice but this method
> is deprecated in 10.6 which is my target system.
>
>
>
>
>
> What would be the best way to have my high res picture print with my
> watermark on it?
>
>
>
> I know from my unsuccessful searches for the past weeks that this theme
> has been addressed several times but I just can't seem to find an easy
> answer to my problem.
>
>
>
> Many thanks for your help,
>
> Olivier./.
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please 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
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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