I have two versions of a program that include a window that has a bar graph in it. One version is written completely in ASOC, while the other has parts of the program (including the graphing part) written in Objective C for faster response to user input. When I try to print the graph window, the one in ASOC prints correctly, while the one in Objective C is distorted – the bars are displaced horizontally and they’re off scale. AS far as I can tell, the Objective C version is a direct translation of the ASOC version, so I can’t figure out why these would behave differently. The graphs look identical on the screen. Does anyone know what is going on here, or is there another way to print the graph window (or just the content view) that would be better? In addition to printing it, I would also like to be able to save the graph as a jpeg or other image file type.
I haven’t tried dataWithPDFInsideRect, but if I click the pdf button in the print window, I get the same bad result.
I did make a test application with a window having just a table, slider and button, and tried to get a jpeg file from the view with the following code:
script PrintingWindowsAppDelegate
property parent : class "NSObject"
property theContentView : missing value
property theImageRep : missing value
property pathToDesktop : POSIX path of (path to desktop as string)
on push_(sender)
set theDict to current application's NSDictionary's dictionaryWithObjectsAndKeys_(1.0, current application's NSImageCompressionFactor, missing value)
set theFrame to theContentView's frame()
set theImageRep to theContentView's bitmapImageRepForCachingDisplayInRect_(theFrame)
set imageData to theImageRep's representationUsingType_properties_(current application's NSJPEGFileType, theDict)
writeToFile_(imageData)
end push_
on writeToFile_(theData)
set picFile to pathToDesktop & "view.jpg"
if theData's writeToFile_atomically_(picFile, true) is 0 then
set messageText to "There was an error writing to file"
display dialog messageText buttons {"Ok"} default button 1
end if
end writeToFile_
end script
This did produce a jpeg file, but it was blank (white). Logging theImageRep gives this:
Logging imageData, gives a long string of numbers. I don’t know if I’m missing the point here with these methods or just need some minor tweak to get this to work.
Shane,
Thanks for the suggestion. I hadn’t really looked closely at cacheDisplayInRect:toBitmapImageRep: method. After checking that one out, it’s clear that bitmapImageRepForCachingDisplayInRect: just initializes the imageRep, and then cacheDisplayInRect:toBitmapImageRep: writes the image data to it. So, these three lines set up the imageRep, load it with data and then make a data object that can be written out to a jpeg file:
set theImageRep to theContentView's bitmapImageRepForCachingDisplayInRect_(theFrame)
theContentView's cacheDisplayInRect_toBitmapImageRep_(theFrame, theImageRep)
set imageData to theImageRep's representationUsingType_properties_(current application's NSJPEGFileType, theDict)
I’m not sure what all that data I saw was when I logged imageData in my original post.
Maybe not irrelevant, but much less of a problem. It still would be nice to just click on the window and choose print from the menu. But the jpeg image is correct and prints properly. I guess I could add a “Print Graph” menu item that would call my code to make the jpeg file and then print it somehow. I also want the users to be able to add this graph to their web page, so having it as a separate jpeg file is useful for that.