 |
 |
Drawing an NSImage
|
 |
|
 |
|
Senior User
Join Date: Jun 1999
Location: San Jose, CA
Status:
Offline
|
|
Ok, I am a bit confused on how to draw NSImages after reading through the NSImage docs. Note I am using Objective-C (not JAVA).
I have a multipage PDF file (bodyPDF) loaded into an NSImage, and a single page PDF file (coverPDF) loaded into an NSImage. I want to "draw" these two NSImages into the same view (or is it rect) so that I can then write it out as one pdf file to disk. I have gotten everything figured out except fo rthe actual "drawing" step. Looking at the NSImage docs I see the following:
- (void)compositeToPoint  NSPoint)aPoint operation  NSCompositingOperation)op
- (void)drawAtPoint  NSPoint)point fromRect  NSRect)srcRect operation  NSCompositingOperation)op fraction  float)delta
Which one do I want to be using here, composite of draw? And what is the point I would be drawing to? Do I need to know that after I draw the first NSImage (coverPDF), I need to know the lower left hand point, and that is the starting point for my next NSImage (bodyPDF)?
My next issue is that the number of pages and sizes (could be letter, legal or A4) are varibale, so I don't know how big of a rect to setup (do I need to setup a rect large enough to hold all my pages, or just the front most?) I understand how to extract the number of pages, and page sizes, just not sure if I need to make the rect that will be "drawn" into large enough to hold them all.
I hope something up there made some sense and someone can help me out a bit.
Thanks for the help,
Ben
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Mar 2001
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by kupan787:
<strong>Ok, I am a bit confused on how to draw NSImages after reading through the NSImage docs. Note I am using Objective-C (not JAVA).
I have a multipage PDF file (bodyPDF) loaded into an NSImage, and a single page PDF file (coverPDF) loaded into an NSImage. I want to "draw" these two NSImages into the same view (or is it rect) so that I can then write it out as one pdf file to disk. I have gotten everything figured out except fo rthe actual "drawing" step. Looking at the NSImage docs I see the following:
- (void)compositeToPoint  NSPoint)aPoint operation  NSCompositingOperation)op
- (void)drawAtPoint  NSPoint)point fromRect  NSRect)srcRect operation  NSCompositingOperation)op fraction  float)delta
Which one do I want to be using here, composite of draw? And what is the point I would be drawing to? Do I need to know that after I draw the first NSImage (coverPDF), I need to know the lower left hand point, and that is the starting point for my next NSImage (bodyPDF)?
My next issue is that the number of pages and sizes (could be letter, legal or A4) are varibale, so I don't know how big of a rect to setup (do I need to setup a rect large enough to hold all my pages, or just the front most?) I understand how to extract the number of pages, and page sizes, just not sure if I need to make the rect that will be "drawn" into large enough to hold them all.
I hope something up there made some sense and someone can help me out a bit.
Thanks for the help,
Ben</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Hi Ben,
You may have a look on the developer documentation from this point : <a href="http://developer.apple.com/techpubs/macosx/Cocoa/TasksAndConcepts/ProgrammingTopics/Misc/DrawingPage.html" target="_blank">http://developer.apple.com/techpubs/macosx/Cocoa/TasksAndConcepts/ProgrammingTopics/Misc/DrawingPage.html</a> (it is the whole Frawing and Imaging Section).
Here is a good start for your invstigations :
- an NSImage is not a view (so it is not an NSView children), it is an object that carry an image data and some filters and operations you can do to the image. NSImage can draw the image it contains with the functions you found : compositeToPoint, drawInRect: fromRect: ...
However, these functions will draw the image at the coordinates you specify (a point for compositeToPoint, and this point is the lower left corner of the image)
The coordinates are given for the CURRENT COORDINATE SYSTEM, it is the current NSView that is focused.
- an NSView is something that have an origin and a size. It is supposed to be somewhere on a superview or on the screen. Each NSView have its own coordinate system.
An exemple, suppose you want to do a custom button. You would subclass NSButton and place this view (NSButton is an NSView) somewhere in a window with Interface Builder.
Then, each NSView have a drawRect method that should contain the code for drawing the view.
You want to have a specific image for the button, for exemple myCustomButtonImage.
you will then have :
- (void)drawRect  NSRect)aRect
{
[myCustomButtonImage compositeToPoint:NSZeroPoint operation:NSCompositeOperationCopy];
}
(please, note that the syntax may not be exact in my exemple)
If you have to draw an image outside of a drawRect method, don't forget to lock the view focus, draw in that view, and unlock its focus.
Ex :
[myView lockFocus];
[myImage compositeToPoint  ...)];
[myView unlockFocus];
Hope this helps.
Regards
|
|
Imagine that my signature is here...
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jun 1999
Location: San Jose, CA
Status:
Offline
|
|
A bit more confused now
So if I want to draw 2 NSImages to the same place, but not onscreen (am I drawing them to a view or where are they being drawn to), what call do I use?
You mentioned that a point for compositeToPoint is the lower left corner of the image. So does that mean that when I am trying to line up to NSImages (one then the other), I need to know the distances between these points?
What I want to do is basicly merge 2 pdf documents into one, and I heard that this was the easiest way (by drawing them both into 1 NSImage, and then writing that to disk). Am I not going about this the right way?
Thanks,
Ben
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
The easiest thing to do is make an instance of a custom subclass of NSView in IB, provide it with the instance variables of type NSImage, and override NSViews drawRect: method.
Something like this:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">@implementation CustomView
- (id)initWithFrame  NSRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
//any initialization u desire
}
return self;
}
-(void)drawRect  NSRect)frame
{
[image1 compositeToPoint:somePoint operation:someOperation];
[image2 compositeToPoint:otherPoint operation:anOperation];
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">You'd have to initilize your image1 and image2, of course, and also pick out an operation (found in the NSImage docs) and a point inside the NSView.
Hope that wasn't too confusing.
F-bacher
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Mar 2001
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by kupan787:
You mentioned that a point for compositeToPoint is the lower left corner of the image. So does that mean that when I am trying to line up to NSImages (one then the other), I need to know the distances between these points?
</font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Yes, you have to know the distance between these points.
Hopefuly, [NSImage size] method will give you this info. You will find info on NSSize on the Foundation functions.
NSSize is a sort of struct containing two items : width and height
Exemple :
[myFirstImage size].height will give you the heignt of your image.
So, to composite the two images, you may do something like
[myFirstImage compositeToPoint:NSZeroPoint operation...];
[mySecondImage compositeToPoint:NSMakePoint(0, [myFirstImage size].height) operation:...];
This exemple will draw the second image on the top of the first one.
Hope I'm not too confusing <img border="0" title="" alt="[Wink]" src="wink.gif" /> ...
|
|
Imagine that my signature is here...
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jun 1999
Location: San Jose, CA
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by hELLO wORLD:
<strong>So, to composite the two images, you may do something like
[myFirstImage compositeToPoint:NSZeroPoint operation...];
[mySecondImage compositeToPoint:NSMakePoint(0, [myFirstImage size].height) operation:...];
This exemple will draw the second image on the top of the first one.
Hope I'm not too confusing <img border="0" title="" alt="[Wink]" src="wink.gif" /> ...</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">But where are they being drawn to? To save to disk, as far as I understand, I would need to grab the NSData, and then writeToDisk, yes?
So I basicly would have something like...
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">[myFirstImage compositeToPoint:NSZeroPoint operation...];
[mySecondImage compositeToPoint:NSMakePoint(0, NSZeroPoint-[myFirstImage size].height) operation:...];</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">And then I would have the two images laid down in succession? So it would look like (my ascii art):
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">|------|
| |
| 1 |
| |
|------|
|------|
| |
| 2 |
| |
|------|</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Thanks,
Ben
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Mar 2001
Status:
Offline
|
|
The images are drawn in the current focused View. I suppose it would be possible to have a virtual NSView and then save its content to disk. However, I would not use an NSView to de what you want. I suppose there is a way to create an NSImage with the composition of the two others, and then you will be able to save it easily (thanks NSImage methods).
You may have to go deep into NSImage and CoreGraphics to see how to do this.
I suppose you can draw in an NSImage since it has lockFocus and unlockFocus methods,
So try to create an NSImage like this (supposing image1 and image2 have the same width) :
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">NSImage *myImageToSave = [NSImage initWithSize:NSMakeSize([image1 size].width, [image1 size].height+[image2 size].height)];
[myImageToSave lockFocus];
[image1 compositeToPoint:NSZeroPoint operation:NSCompositeCopy];
[image2 compositeToPoint:NSMakePoint(0, [image1 size].height) operation:NSCompositeCopy];
[myImageToSave unlockFocus];
// then you can save your image to disk</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Please, note that I have not tested this code, and I can be completly wrong. I have never tried yet to compose an image from two others...
Hope this will work. Post the result if you can.
Regards
|
|
Imagine that my signature is here...
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jun 1999
Location: San Jose, CA
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by hELLO wORLD:
<strong>Please, note that I have not tested this code, and I can be completly wrong. I have never tried yet to compose an image from two others...
Hope this will work. Post the result if you can.
Regards</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">This is almost perfect! The only thing I need to now account for is page size (US Letter is 792 pixels high). Lets say, for example, I have two images, one is height 792, and the other is height 450. I want to have my final image be 2 pages, so I need to do a bit of formatting.
I tried soemthign like this:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">image1 = [[NSImage alloc] initWithContentsOfFile:...];
image2 = [[NSImage alloc] initWithContentsOfFile:...];
numPages = ([image1 size].height)/792;
numPages++;
numPages2 = ([image2 size].height)/792;
numPages2++;
myImageToSave = [[NSImage alloc] initWithSize:NSMakeSize([image1 size].width, numPages*792+numPages2*792];
[myImageToSave lockFocus];
[image1 compositeToPoint:NSZeroPoint operation:NSCompositeCopy];
[image2 compositeToPoint:NSMakePoint(0, numPages2*792) operation:NSCompositeCopy];
[myImageToSave unlockFocus];</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">This doesn't seem to change it, as I am still getting the 2nd image with no "white space" around it (as filler, so that it fills the whole area).
Do you get what I am saying here now? If not, I can try and explain in a different way.
Ben
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Mar 2001
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by kupan787:
<strong></font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">[image2 compositeToPoint:NSMakePoint(0, numPages2*792) operation:NSCompositeCopy];</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif"></strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Since the point is the lower left corner of the image to be drawn, I think this line should be :
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">[image2 compositeToPoint:NSMakePoint(0, numPages*792) operation:NSCompositeCopy];</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">I see several solution to your problem.
1) You can keep the rest of your division (the modulo), and divide it by 2 to place the smaller image in the center a rect with the US page size.
The height of the bottom of the image2 would be (supposing image2 height is smaller than 792):
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">int image2Bottom = (numPages * 792) + ( ([image2 size].height % 792) / 2 );</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">2) You can resize your image using NSImage's drawInRect: fromRect:... method.
Here is a graphical explaination of this :
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">2) |--------------| 1) |--------------|
| | |//////////////|
| Image 2 | |--------------|
| scaled to | | |
| US LETTER | | Image 2 |
| SIZE | | unscaled |
| | |--------------|
| | |//////////////|
|--------------| |--------------|
| | | |
| Image 1 | | Image 1 |
| | | |
| US LETTER | | US LETTER |
| SIZE | | SIZE |
| | | |
| | | |
|--------------| |--------------|</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">
|
|
Imagine that my signature is here...
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jun 1999
Location: San Jose, CA
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by hELLO wORLD:
<strong>I see several solution to your problem.
1) You can keep the rest of your division (the modulo), and divide it by 2 to place the smaller image in the center a rect with the US page size.
The height of the bottom of the image2 would be (supposing image2 height is smaller than 792):
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">int image2Bottom = (numPages * 792) + ( ([image2 size].height % 792) / 2 );</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif"></strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">This is the way to go. My only question is where would I calculate this factor in? Would it go like this:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">[image2 compositeToPoint:NSMakePoint(0, image2Bottom) operation:NSCompositeCopy];</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif"></font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif"><strong>
2) You can resize your image using NSImage's drawInRect: fromRect:... method.
Here is a graphical explaination of this :
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">2) |--------------| 1) |--------------|
| | |//////////////|
| Image 2 | |--------------|
| scaled to | | |
| US LETTER | | Image 2 |
| SIZE | | unscaled |
| | |--------------|
| | |//////////////|
|--------------| |--------------|
| | | |
| Image 1 | | Image 1 |
| | | |
| US LETTER | | US LETTER |
| SIZE | | SIZE |
| | | |
| | | |
|--------------| |--------------|</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif"></strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">I cant do it this way do to the faxct that it could be text and if that got scaled, it would become unreadable. But your first method sounds like it could work!
Also, I don't know how much you would know about this but I read up on the compressions I can use (when I save this back out to disk as tiff), and I found the one I need:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif"> NSTIFFCompressionCCITTFAX3
CCITT Fax Group 3 compression. It's for one-bit fax images sent over telephone lines.</font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">However, when I do this:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">
myTiffData = [myImageToSave TIFFRepresentationUsingCompression:NSTIFFCompressi onCCITTFAX3 factor:nil];
[myTiffData writeToFile:@"/tmp/send/Output.tif" atomically:YES];</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">The resulting image is a full color tiff (it looks exactly like the pdf file). From my understanding, I should have gotten a 1-bit, black and white image. What went wrong? Did I read into this too much, or am I not saving it with CCITT Fax Group 3 compression? Hopefully you know a bit about this, or someoen else that is readin gthis knows about the different tiff compressions.
Thanks,
Ben
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Mar 2001
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by kupan787:
<strong>(…)
This is the way to go. My only question is where would I calculate this factor in? Would it go like this:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">[image2 compositeToPoint:NSMakePoint(0, image2Bottom) operation:NSCompositeCopy];</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif"></strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Yes, I think it is the way to go
For you positioning problem, all you have to do now is purely arithmetic
For your compression problem, I really don't know. As I said, I have never used NSImage deeply, and I am happy that the code I give you works well. It seems I begin to have the Cocoa logic in my blood <img border="0" title="" alt="[Wink]" src="wink.gif" />
You may have a look further in the developer documentation. Sometimes, there is solutions hidden somewhere in a forgotten link… It is easy to go crazy sometimes when looking for info I found in the docs and I am unable to find again…
Maybe there is sample code for this… Surely someone here would know the solution, and there are also Apple developer forums and the Omni Mailing list.
To end, try to search NSImage TIFF, compression with Google, you may find exemples or even better a piece of code !
Try also the stepwise website, I think they have links to exemples and open source code.
|
|
Imagine that my signature is here...
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jun 1999
Location: San Jose, CA
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by hELLO wORLD:
<strong> </font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by kupan787:
<strong>(…)
This is the way to go. My only question is where would I calculate this factor in? Would it go like this:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">[image2 compositeToPoint:NSMakePoint(0, image2Bottom) operation:NSCompositeCopy];</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif"></strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Yes, I think it is the way to go
</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">
Problem now is that ProjectBuilder is giving me an error on the code:
image2Bottom = (numPages * 792) + ( ([image2 size].height % 792) / 2 );
It says, invalid operands to binary %
So I guess it doesn't like your modulus (sp?) sign in there. What does that do again, return the remainder?
Something else I was just thinking, after I merge the file, I save it to disk as a tiff, and then I use that one file (but it is properly formatted as different pages). Woudl it be easier, you think, to just spit out each page of the file to disk? Then I don't care if the page is correct heght or not.
Basicly what I mean is I would have 2 pdf files (the first one is one page, and the second one is 3 pages). I want to conert the 2 pdf files to tif, and then save them to disk as each page its own tiff file. So how would I get at the individual pages in the pdf file? I am guessing I would need to just draw into an NSImage that was 792 pixels high, but how would I position the image in that? Can I use negative coords?
Hopefully that last bit made sense.
Thanks,
Ben
<small>[ 06-21-2002, 02:33 PM: Message edited by: kupan787 ]</small>
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Mar 2001
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by kupan787:
<strong>
No go. The image I get is still not "white spaced"
</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Maybe you have to draw the white space between the two images.
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">
<strong>
Something else I was just thinking, after I merge the file, I save it to disk as a tiff, and then I use that one file (but it is properly formatted as different pages). Woudl it be easier, you think, to just spit out each page of the file to disk? Then I don't care if the page is correct heght or not.
Basicly what I mean is I would have 2 pdf files (the first one is one page, and the second one is 3 pages). I want to conert the 2 pdf files to tif, and then save them to disk as each page its own tiff file. So how would I get at the individual pages in the pdf file? I am guessing I would need to just draw into an NSImage that was 792 pixels high, but how would I position the image in that? Can I use negative coords?
</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">This is maybe the better way to go for tour needs, and the proper method would be :
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">- (void)drawAtPoint  NSPoint)point fromRect  NSRect)srcRect operation  NSCompositingOperation)op fraction  float)delta</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">You may want to send 1.0 as delta since you don't want transparency.
the point would be NSZeroPoint since you will draw each page in separate NSImages with the US Letter size.
What is important here, and useful for you is fromRect.
Here you send the rect of the portion of the source image you want to draw.
So you would have something like this (ex. is for page 2) :
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">//somewhere before in the header
#define kUSLetterWidth 'put the width here'
#define kUSLetterHeight 792
static NSSize kUSLetterSize = NSMakeSize(kUSLetterWidth, kUSLetterHeight);
//in your implementation
NSImage *myPage2ToSave = [NSImage initWithSize: kUSLetterSize];
int bottomOfPage2InMyImageWith3Pages = (kUSLetterHeight * 2) - kUSLetterHeight;
// we substract kUSLetterHeight since we want the bottom of the page and NSImage takes the lower-left corner... I suppose your pages are disposed, from bottom to top in the source image. You may have to change this if they are disposed from top to bottom...
NSRect rectOfPage2InMyImageWith3Pages = NSMakeRect(0, bottomOfPage2InMyImageWith3Pages, kUSLetterWidth, kUSLetterHeight+bottomOfPage2InMyImageWith3Pages);
[myPage2ToSave lockFocus];
[myImageWith3pages
drawAtPoint:NSZeroPoint
fromRect:rectOfPage2InMyImageWith3Pages
operation:NSCompositeCopy
fraction:1.0
];
[myPage2ToSave unlockFocus];
// then you can save your image to disk</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Is this what you were looking for ?
|
|
Imagine that my signature is here...
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Mar 2001
Status:
Offline
|
|
Argh... You edited your post when I was replying to it !
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by kupan787:
<strong>Problem now is that ProjectBuilder is giving me an error on the code:
image2Bottom = (numPages * 792) + ( ([image2 size].height % 792) / 2 );
It says, invalid operands to binary %
So I guess it doesn't like your modulus (sp?) sign in there. What does that do again, return the remainder?
</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">a%b returns the remainder that occurs after performing a/b. For this operator, a and b must be integers.
Try to cast the image2 height to integer.
image2Bottom = (numPages * 792) + ( ( (int)[image2 size].height % 792 ) / 2 );
Also, use the debugger ! Try to catch [image2 size].height to see if it is taller than 792
Or simply use logs :
NSLog(@"Height of image 2: %i",(int)[image2 size].height);
NSLog(@"Modulus result: %i",(int)[image2 size].height % 792);
|
|
Imagine that my signature is here...
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|