I have a number of questions concerning the specific code needed to dynamically generate, display, print, and save diagrams (images) in Cocoa/Objective-C. I am relatively new to Objective-C and Cocoa, and I am not fnding it easy to track down the source of information that would permit me to develop an understanding of how to solve these problems. I spent all of yesterday looking for answers on the web and, while I found some, I have not been able to solve all of the problems, nor do I understand all of the issues.
Problem 1: I want to dynamically generate a diagram in code and then either display it on the screen, print it, or save it to a disk file for display on a web site. The diagram in this instane is a series of lines and decorative text that represent a single- or double-elimination tournament for any sport (Little League Baseball in this case), so the lines show each game and how the winners advance to the final game and the chanpionship. Text on the diagram includes the tournament title and other information, the team names and game results, game labels, and instructions (such as "loser to D5"). I should note that I have written this program in other languages, it is my attempt to achieve this in Objective-C/Cocoa that is my stumbling block.
From several different programming models I am acustomed to their being present a set of classes/procedures that can be called to 'draw' into the current graphics object, and then methods that can capture the set of drawing results and display/print/save them. The basic set of tools are points, lines, rectangles, ovals, arcs, paths, and text.
My research has lead me to the NSImage class, which it would seem that I should be able to use to generate these diagrams. I even found some sample code that used NSImage to generate a small 'picture' and save it as a TIFF file; there were also some mentions of other formats, but the material is quite vahue at that point. I was able to create and save a diagram using the following template:
float resolution = 300.0;
_Diagram = [[NSImage alloc] initWithSize:NSMakeSize(8.5*resolution, 11.0*resolution)];
[_Diagram lockFocus];
// draw the diagram
[_Diagram unlockFocus];
NSData* tiffData = [_Diagram TIFFRepresentation];
[tiffData writeToFile:[NSString stringWithFormat"/Users/DiagramExperiment/diagram.tiff"] atomically:NO];
Unfortunately the only drawing commands I could find are NSBexierPath and [NSString drawAtPoint] and related methods. In order to draw a simple line I need to program it as a bezier path with two endpoints! This seems excessive.
Question 1: Are there other drawing tools aside from bezier paths and drawing strings?
Question 2: Scaling and sizing. I wanted my diagram to appear as if it were being drawn on a standard piece of paper, 8.5x11 inches, and so I size the image in that manner, but I multiply it by the desired resolution. Is this the appropriate method for generating the image at 300dpi? When I save the file it is 35.42x45.83 inches at 72dpi; how can I change the above code so the saved file will be 8.5x11 inches at 300dpi?
Question 3: If I add some code to the above that draws three (3) lines (3 pixels thick) and draws two strings ("Team 1" and "Team 2") before the unlockFocus, then the saved TIFF file is 32.1MB in size! The documentation on other representations are quite confusing, but my old (REALbasic) code saved much more complicated diagrams that were only 28KB in size (but it turns out that they were being saved only at 72dpi, which is probably fine for the web). How can I create the image so that it is 72dpi 8.5x11 on the screen and when saved to a file, but is 200dpi 8.5x11 when I want to print it?
Problem 1: I want to dynamically generate a diagram in code and then either display it on the screen, print it, or save it to a disk file for display on a web site. The diagram in this instane is a series of lines and decorative text that represent a single- or double-elimination tournament for any sport (Little League Baseball in this case), so the lines show each game and how the winners advance to the final game and the chanpionship. Text on the diagram includes the tournament title and other information, the team names and game results, game labels, and instructions (such as "loser to D5"). I should note that I have written this program in other languages, it is my attempt to achieve this in Objective-C/Cocoa that is my stumbling block.
From several different programming models I am acustomed to their being present a set of classes/procedures that can be called to 'draw' into the current graphics object, and then methods that can capture the set of drawing results and display/print/save them. The basic set of tools are points, lines, rectangles, ovals, arcs, paths, and text.
My research has lead me to the NSImage class, which it would seem that I should be able to use to generate these diagrams. I even found some sample code that used NSImage to generate a small 'picture' and save it as a TIFF file; there were also some mentions of other formats, but the material is quite vahue at that point. I was able to create and save a diagram using the following template:
float resolution = 300.0;
_Diagram = [[NSImage alloc] initWithSize:NSMakeSize(8.5*resolution, 11.0*resolution)];
[_Diagram lockFocus];
// draw the diagram
[_Diagram unlockFocus];
NSData* tiffData = [_Diagram TIFFRepresentation];
[tiffData writeToFile:[NSString stringWithFormat"/Users/DiagramExperiment/diagram.tiff"] atomically:NO];
Unfortunately the only drawing commands I could find are NSBexierPath and [NSString drawAtPoint] and related methods. In order to draw a simple line I need to program it as a bezier path with two endpoints! This seems excessive.
Question 1: Are there other drawing tools aside from bezier paths and drawing strings?
Question 2: Scaling and sizing. I wanted my diagram to appear as if it were being drawn on a standard piece of paper, 8.5x11 inches, and so I size the image in that manner, but I multiply it by the desired resolution. Is this the appropriate method for generating the image at 300dpi? When I save the file it is 35.42x45.83 inches at 72dpi; how can I change the above code so the saved file will be 8.5x11 inches at 300dpi?
Question 3: If I add some code to the above that draws three (3) lines (3 pixels thick) and draws two strings ("Team 1" and "Team 2") before the unlockFocus, then the saved TIFF file is 32.1MB in size! The documentation on other representations are quite confusing, but my old (REALbasic) code saved much more complicated diagrams that were only 28KB in size (but it turns out that they were being saved only at 72dpi, which is probably fine for the web). How can I create the image so that it is 72dpi 8.5x11 on the screen and when saved to a file, but is 200dpi 8.5x11 when I want to print it?