Re: Using NSImage to create high res images on disk for use in a webview (that will be printed)
Re: Using NSImage to create high res images on disk for use in a webview (that will be printed)
- Subject: Re: Using NSImage to create high res images on disk for use in a webview (that will be printed)
- From: George Orthwein <email@hidden>
- Date: Thu, 20 Apr 2006 23:17:20 -0400
On Apr 14, 2006, at 11:48 AM, Mike Zornek wrote:
Hi everyone, and thanks in advance for reading my post.
So I have a Cocoa app that generates PDFs. It does this by
generating HTML,
displaying that HTML in an off-screen WebView, and then uses the print
system to create a paginated PDF based on that WebView.
At the top of the PDF I have the company's name being printed, but
I want to
give them the option of placing their logo up there. My thought is
to add an
image well to the preferences window and have them set their logo;
then in
code I will generate a file on disk (in ~/Libray/Caches/MyApp/) so
that the
WebView can render an IMG tag with a SRC path that points to
"~/Libray/Caches/MyApp/company_logo.png".
One neat-o thing you can do is actually display image data in memory
directly in the WebView by using addSubResource. I personally like
the idea of not having to save a file to disk:
http://lists.apple.com/archives/Webkitsdk-dev/2005/Aug/msg00020.html
(There is a bug when redefining the image data for a particular url
but it shouldn't affect you.)
Now this logo needs to be a high res logo since it will printed and
I'm
having a hard time understanding what I need to do to accommodate the
various image types and resolutions that may be dropped in that
image well.
My current code to generate the file is:
// before we create the PDF let's make a PNG file for the company logo
NSImage *logo = [NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults
standardUserDefaults] valueForKey:CBCompanyLogo]];
[[[NSBitmapImageRep imageRepWithData:[logo TIFFRepresentation]]
representationUsingType:NSPNGFileType properties:nil] writeToFile:
[self
pathToLogoCache] atomically:YES];
Here's my understanding: (I'm no expert, so take the following with a
grain of salt...)
The NSImageView will get you an NSImage which contains NSImageReps. A
tiff or jpg dropped on the NSImageView will give you an NSImage with
an NSBitmapImageRep. An eps will give you one with an NSEPSImageRep.
Just NSLog the NSImage to quickly see what NSImageReps it contains.
For NSBitmapImageReps (jpg,tiffs, etc), you probably don't need to
resize the image for your purposes (unless it's huge and you want to
downscale it.) If you do want to resize it, you can look at Matt
Gemmell's code which looks like it draws the NSImage into a new,
blank NSImage at a different size. Also see "Drawing to an Image":
http://developer.apple.com/documentation/Cocoa/Conceptual/
CocoaDrawingGuide/Images/chapter_7_section_5.html
Contrary to what you might think, you don't want to use NSImage's
setSize: because the interpolation isn't very good and you will get
jaggies.
For NSEPSImageReps, forget what I just said because you actually DO
want to embrace setSize. When scaling it using the method above
(drawing to an new NSImage), it seems to render a 72dpi version first
based on the pt dimensions in the eps file and then draws that at the
new size. There are probably other ways of dealing with this
(drawRepresentation:inRect:?) but I have used setSize successfully.
Since it is a vector, it resizes fine. Something like:
NSImage *img = [sourceImageView image];
[img setScalesWhenResized:YES];
[img setSize:NSMakeSize(1000.0,1000.0)];
NSBitmapImageRep *myBitmapImageRep = [NSBitmapImageRep
imageRepWithData:[img TIFFRepresentation]];
Also note here TIFFRepresentation is used to convert the
NSEPSImageRep to a NSBItmapImageRep. If you already have an NSImage
with an NSBitmapImageRep it may be a little redundant. You can just
access the already existing NSBitmapImageRep with:
NSImageRep *myBitmapImageRep = [img bestRepresentationForDevice:nil];
Hope that helps...
George
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden