ios - How to create circular CGRect -
i using code create rectangle in app. want make rounded. how can that.
- (void)setcroprect:(cgrect)croprect { if(!cgrectequaltorect(_croprect,croprect)){ _croprect = cgrectoffset(croprect, self.frame.origin.x, self.frame.origin.y); uigraphicsbeginimagecontextwithoptions(self.bounds.size, no, 0.f); cgcontextref context = uigraphicsgetcurrentcontext(); [[uicolor blackcolor] setfill]; uirectfill(self.bounds); cgcontextsetstrokecolorwithcolor(context, [[uicolor whitecolor] colorwithalphacomponent:0.5].cgcolor); cgcontextstrokerect(context, croprect); [[uicolor clearcolor] setfill]; uirectfill(cgrectinset(croprect, 1, 1)); self.imageview.image = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); } }
i using code crop image in rectangular shape:
- (cgimageref)newtransformedimage:(cgaffinetransform)transform sourceimage:(cgimageref)sourceimage sourcesize:(cgsize)sourcesize sourceorientation:(uiimageorientation)sourceorientation outputwidth:(cgfloat)outputwidth croprect:(cgrect)croprect imageviewsize:(cgsize)imageviewsize { cgimageref source = sourceimage; cgaffinetransform orientationtransform; [self transform:&orientationtransform andsize:&imageviewsize fororientation:sourceorientation]; cgfloat aspect = croprect.size.height/croprect.size.width; cgsize outputsize = cgsizemake(outputwidth, outputwidth*aspect); cgcontextref context = cgbitmapcontextcreate(null, outputsize.width, outputsize.height, cgimagegetbitspercomponent(source), 0, cgimagegetcolorspace(source), cgimagegetbitmapinfo(source)); cgcontextsetfillcolorwithcolor(context, [[uicolor clearcolor] cgcolor]); cgcontextfillrect(context, cgrectmake(0, 0, outputsize.width, outputsize.height)); cgaffinetransform uicoords = cgaffinetransformmakescale(outputsize.width/croprect.size.width, outputsize.height/croprect.size.height); uicoords = cgaffinetransformtranslate(uicoords, croprect.size.width/2.0, croprect.size.height/2.0); uicoords = cgaffinetransformscale(uicoords, 1.0, -1.0); cgcontextconcatctm(context, uicoords); cgcontextconcatctm(context, transform); cgcontextscalectm(context, 1.0, -1.0); cgcontextconcatctm(context, orientationtransform); cgcontextdrawimage(context, cgrectmake(-imageviewsize.width/2.0, -imageviewsize.height/2.0, imageviewsize.width, imageviewsize.height) ,source); cgimageref resultref = cgbitmapcontextcreateimage(context); cgcontextrelease(context); return resultref; }
is can use brazier curve crop image also.
you cannot create “circular cgrect”, or rounded cgrect (which appear want). cgrect has origin , width , height. doesn't store corner radiuses (radii).
instead of using uirectfill
, need create uibezierpath
of rounded rect , fill path. example:
uibezierpath *path = [uibezierpath bezierpathwithroundedrect:self.bounds cornerradius:10]; [[uicolor blackcolor] setfill]; [path fill];
you can stroke path if need to. example:
path = [uibezierpath bezierpathwithroundedrect:croprect cornerradius:10]; path.linewidth = 10; [[[uicolor whitecolor] colorwithalphacomponent:0.5] setstroke]; [path stroke];
Comments
Post a Comment