ios - UIGraphicsGetImageFromCurrentContext and preserve aspect ratio -


i'm trying implement uiimageview subclass allow user draw in image view using fingers. contentmode of uiimageview set aspect fill. noticed when draw code executes , resulting image extracted graphics context being scaled in scale fill type format not want. wondering if knows how achieve extracting image , maintaining aspect ratio of image.

class drawimageview : uiimageview {      var lasttouchpoint = cgpoint.zero     var isswiping = false      override func touchesbegan(_ touches: set<uitouch>, event: uievent?) {         isswiping = false          if let touchpoint = touches.first {             lasttouchpoint = touchpoint.location(in: self)         }     }      override func touchesmoved(_ touches: set<uitouch>, event: uievent?) {         isswiping = true          if let touchpoint = touches.first {             let currentpoint = touchpoint.location(in: self)             uigraphicsbeginimagecontext(frame.size)              image?.draw(in: cgrect(x:0, y:0, width:frame.size.width, height:frame.size.height))              if let context = uigraphicsgetcurrentcontext() {                 context.move(to: lasttouchpoint)                 context.addline(to: currentpoint)                 context.setlinecap(cglinecap.round)                 context.setlinewidth(9.0)                 context.setstrokecolor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0)                 context.strokepath()                 image = uigraphicsgetimagefromcurrentimagecontext()                 uigraphicsendimagecontext()                 lasttouchpoint = currentpoint             }         }     }      override func touchesended(_ touches: set<uitouch>, event: uievent?) {         if(!isswiping) {              uigraphicsbeginimagecontext(frame.size)             image?.draw(in: cgrect(x:0, y:0, width:frame.size.width, height:frame.size.height))               if let context = uigraphicsgetcurrentcontext() {                 context.setlinecap(cglinecap.round)                 context.setlinewidth(9.0)                 context.setstrokecolor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0)                 context.move(to: lasttouchpoint)                 context.addline(to: lasttouchpoint)                 context.strokepath()                 image = uigraphicsgetimagefromcurrentimagecontext()                 uigraphicsendimagecontext()             }         }     } } 

with code:

uigraphicsbeginimagecontext(frame.size) image?.draw(in: cgrect(x:0, y:0, width:frame.size.width, height:frame.size.height)) 

you saying "draw full image - regardless of size or aspect ratio - rectangle of width x height dimensions." so, if image has "native" size of, say, 300 x 200, , frame 200 x 200, that's image going drawn.

to avoid that, need calculate proper rect draw into. using sizes, you'd want (for aspect-fill):

image?.draw(in: cgrect(x:-50, y:0, width:frame.size.width + 50, height:frame.size.height)) 

of course, instead of hard-coded values, you'd run quick calculations determine actual numbers.


Comments