objective c - CoreGraphics draw an image on a white canvas -
i have source image has variable width , height, must show on fullscreen ipad uiimageview addition of borders around image itself. task create new image white border around it, not overlapping on image itself. i'm doing overlapping via code:
- (uiimage*)imagewithborderfromimage:(uiimage*)source { cgsize size = [source size]; uigraphicsbeginimagecontext(size); cgrect rect = cgrectmake(0, 0, size.width, size.height); [source drawinrect:rect blendmode:kcgblendmodenormal alpha:1.0]; cgcontextref context = uigraphicsgetcurrentcontext(); cgcontextsetrgbstrokecolor(context, 1.0, 1.0, 1.0, 1.0); cgcontextsetlinewidth(context, 40.0); cgcontextstrokerect(context, rect); uiimage *testimg = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); return testimg; } can tell me how first draw white canvas 40 pixels bigger in each direction source image , draw image on it?
i adjusted code make work. does:
- set canvas size size of image + 2*margins
- fill whole canvas background color (white in case)
- draw image in appropriate rectangle (with initial size , required margins)
resulting code is:
- (uiimage*)imagewithborderfromimage:(uiimage*)source { const cgfloat margin = 40.0f; cgsize size = cgsizemake([source size].width + 2*margin, [source size].height + 2*margin); uigraphicsbeginimagecontext(size); [[uicolor whitecolor] setfill]; [[uibezierpath bezierpathwithrect:cgrectmake(0, 0, size.width, size.height)] fill]; cgrect rect = cgrectmake(margin, margin, size.width-2*margin, size.height-2*margin); [source drawinrect:rect blendmode:kcgblendmodenormal alpha:1.0]; uiimage *testimg = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); return testimg; }
Comments
Post a Comment