"ios to mac graphiccontext explanation/conversion" Code Answer

1

here's a simple example that draws a blue circle into an nsimage (i'm using arc in this example, add retains/releases to taste)

nssize size = nsmakesize(50, 50);

nsimage* im = [[nsimage alloc] initwithsize:size];
nsbitmapimagerep* rep = [[nsbitmapimagerep alloc]
                            initwithbitmapdataplanes:null
                                          pixelswide:size.width
                                          pixelshigh:size.height
                                       bitspersample:8
                                     samplesperpixel:4
                                            hasalpha:yes
                                            isplanar:no
                                      colorspacename:nscalibratedrgbcolorspace
                                         bytesperrow:0
                                        bitsperpixel:0];

[im addrepresentation:rep];

[im lockfocus];

cgcontextref ctx = [[nsgraphicscontext currentcontext] graphicsport];
cgcontextclearrect(ctx, nsmakerect(0, 0, size.width, size.height));
cgcontextsetfillcolorwithcolor(ctx, [[nscolor bluecolor] cgcolor]);
cgcontextfillellipseinrect(ctx, nsmakerect(0, 0, size.width, size.height));

[im unlockfocus];

[[im tiffrepresentation] writetofile:@"/users/username/desktop/foo.tiff" atomically:no];

the main difference is that on os x you first have to create the image, then you can begin drawing into it; on ios you create the context, then extract the image from it.

basically, lockfocus makes the current context be the image and you draw directly onto it, then use the image.

i'm not completely sure if this answers all of your question, but i think it's at least one part of it.

By Mr.GT on July 13 2022

Answers related to “ios to mac graphiccontext explanation/conversion”

Only authorized users can answer the Search term. Please sign in first, or register a free account.