objective c - How do I rotate an image drawn directly to an NSView with DrawRect in an OSX app? -


i'm writing osx app in xcode using objective-c. have window, nsview inside it, , nsview supposed use data nsmutablearray containing nsnumbers draw corresponding images on grid such images drawn @ 0,0; 32,0; 64,0 . . . 0,32; 32,32; etc. accordingly array's count grid's w*h, in case 21*21 or 441.

you left click "place" image, means updating array based on clicked , calling setneedsdisplay:yes redraws reflect updated array. far, can draw images based on array properly.

when right click, though, supposed rotate image in particular grid slot amount. problem having here figuring out how draw rotated images, in proper locations. should rotate center points, relative coordinates of 16,16 (all images 32x32 pixels in size). is, code is:

- (void)drawrect:(nsrect)dirtyrect {     [super drawrect:dirtyrect];      //black background     [[nscolor blackcolor] setfill];     nsrectfill(dirtyrect);      // drawing code here.     nsrect recttodraw = cgrectmake(0,0,32,32);     nsinteger imageindex = 0;     nsimage *imagetodraw = nil;     (int = 0; < [objdataarray count]; i++) {         //loop through data array , draw each image should         if ([objdataarray objectatindex:i]==[nsnull null]) continue; //don't draw in slot          //math determine draw based on current loop index         //0 = 0,0; 1 = 32,0 . . . 20 = 640,0; 21 = 0,32; etc. (grid 21x21)         recttodraw.origin.x = (i % 21)*32;         recttodraw.origin.y = floor(i/21)*32;          //get data @ index in data array         imageindex = [((nsnumber*)[objdataarray objectatindex:i]) integervalue];          //use retrieved number corresponding image         imagetodraw = (nsimage*)[objimagesarray objectatindex:imageindex];          //draw image @ proper location         [imagetodraw drawinrect:recttodraw];     } } 

so amount of rotation in degrees specified variable rotationamount. how change drawinrect line (the last line before closing braces) image draws @ proper location specified recttodraw, rotated rotationamount degrees center?

thanks.

you don't draw image rotated, such. transform coordinate space , draw image.

[nsgraphicscontext savegraphicsstate];  nsaffinetransform* xform = [nsaffinetransform transform];  // translate image's center view origin rotation occurs around it. [xform translatexby:-nsmidx(recttodraw) yby:-nsmidy(recttodraw)]; [xform rotatebydegrees:rotationamount]; [xform concat];  [imagetodraw drawinrect:nsoffsetrect(recttodraw, -nsmidx(recttodraw), -nsmidy(recttodraw))];  [nsgraphicscontext restoregraphicsstate]; 

there's chance have transform backward. forget way goes (if it's transforming view or content). if image goes off never-never land, change signs of translation.


Comments

Popular posts from this blog

php - render data via PDO::FETCH_FUNC vs loop -

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

The canvas has been tainted by cross-origin data in chrome only -