ios - CGContextRotateCTM not working for rotating a UIImage -
i want draw delete button delete app button on home screen below code.
the idea draw cross first, , rotate 45 degree. what's wrong code?
self.deletebutton = [[uibutton alloc] initwithframe:cgrectmake(0, 0, badge_size, badge_size)]; if (!deletebtnimg) { cgrect imgframe = self.deletebutton.bounds; uigraphicsbeginimagecontextwithoptions(imgframe.size, no, 0.0); cgcontextref context = uigraphicsgetcurrentcontext(); cgcontextsavegstate(context); cgfloat size = min(imgframe.size.width, imgframe.size.height); uibezierpath *path = [uibezierpath bezierpathwitharccenter:cgpointmake(imgframe.size.width/2, imgframe.size.height/2) radius:size/2-radius_margin startangle:0 endangle:m_pi * 2 clockwise:yes]; [path movetopoint:cgpointmake(size / 2, 0)]; [path addlinetopoint:cgpointmake(size / 2, size)]; [path movetopoint:cgpointmake(0, size / 2)]; [path addlinetopoint:cgpointmake(size, size / 2)]; [[uicolor whitecolor] setfill]; [[uicolor redcolor] setstroke]; [path setlinewidth:1.0]; [path fill]; [path stroke]; cgcontextrotatectm(context, m_pi/4); deletebtnimg = uigraphicsgetimagefromcurrentimagecontext(); cgcontextrestoregstate(context); uigraphicsendimagecontext(); } [self.deletebutton setimage:deletebtnimg forstate:uicontrolstatenormal];
you have rotate context before start drawing. if rotate before drawing. image still not right. because when rotate context, context rotating around it's origin point (0,0) or bottom-left corner (the coregraphic's coordinate system bit different ui's). is, before rotation, translate context rotate around center, , move after rotation. here quick example:
cgcontextref context = uigraphicsgetcurrentcontext(); cgcontextsavegstate(context); cgfloat size = min(imgframe.size.width, imgframe.size.height); cgcontexttranslatectm(context, size / 2, size / 2); cgcontextrotatectm(context, m_pi_4); cgcontexttranslatectm(context, -size / 2, -size / 2); // start drawing code
Comments
Post a Comment