i'm adding rectangle image in case it's not squared "filling" remaining space, making square. idea cheap approach (in terms of time required "force" image squared).
as can see in image attached, square generate not have color, , might confuse user. why i'd add color cgrect i'm generating. problem i've not found way this. suggestions on how give color square generated, greately appreciated.
the current code have following:
func forcesquaredimage(image: uiimage) -> uiimage{ let maxdimension = max(image.size.height, image.size.width) var newimage : uiimage = image if(image.size.height > image.size.width){ uigraphicsbeginimagecontextwithoptions(cgsize(width: maxdimension, height: maxdimension), false, 0.0); image.drawinrect(cgrectmake(image.size.height/2-image.size.width/2, 0, image.size.width, image.size.height)) newimage = uigraphicsgetimagefromcurrentimagecontext() uigraphicsendimagecontext() }else if(image.size.height < image.size.width){ uigraphicsbeginimagecontextwithoptions(cgsize(width: maxdimension, height: maxdimension), false, 0.0); image.drawinrect(cgrectmake(0, image.size.width/2-image.size.height/2, image.size.width, image.size.height)) newimage = uigraphicsgetimagefromcurrentimagecontext() uigraphicsendimagecontext() return newimage } return newimage }
the desired result (color red example in case):
just fill background color before draw image:
func forcesquaredimage(image: uiimage) -> uiimage{ let maxdimension = max(image.size.height, image.size.width) var newimage : uiimage = image uigraphicsbeginimagecontextwithoptions(cgsize(width: maxdimension, height: maxdimension), false, 0.0); uicolor.redcolor().set() uibezierpath(rect: cgrect(x:0, y: 0, width:maxdimension, height:maxdimension)).fill() if(image.size.height > image.size.width) { image.drawinrect(cgrectmake(image.size.height/2-image.size.width/2, 0, image.size.width, image.size.height)) } else if (image.size.height < image.size.width) { image.drawinrect(cgrectmake(0, image.size.width/2-image.size.height/2, image.size.width, image.size.height)) } newimage = uigraphicsgetimagefromcurrentimagecontext() uigraphicsendimagecontext() return newimage }
Comments
Post a Comment